vpu_live4.py 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760
  1. #!/usr/bin/python3
  2. import math
  3. import random
  4. import time
  5. import os
  6. import sys
  7. import traceback
  8. import gc
  9. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  10. from lib.cprint import cprint
  11. HOME = os.getenv('HOME')
  12. import _thread as thread
  13. from optparse import OptionParser
  14. ...
  15. parser = OptionParser()
  16. parser.add_option("-m", "--mode", dest="mode",
  17. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  18. parser.add_option("-X", "--XX", dest="XX", #default=1,
  19. help="x-split") #, metavar="FILE")
  20. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  21. help="x-split") #, metavar="FILE")
  22. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  23. help="y-split") #, metavar="FILE")
  24. parser.add_option("", "--start-univ", dest="start_univ",default=2,
  25. help="set start-univers default=2") #, metavar="FILE")
  26. parser.add_option("", "--gobo-ch", dest="gobo_ch",default=0,
  27. help="gobo ch univ on 1") #, metavar="FILE")
  28. parser.add_option("", "--gobo-ch2", dest="gobo_ch2",default=0,
  29. help="gobo ch univ on 1") #, metavar="FILE")
  30. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  31. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  32. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  33. # PIXEL_MAPPING ... for gobo
  34. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  35. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  36. parser.add_option("", "--dual-vpu", dest="dual_vpu",default=0,
  37. help=" --dual-vpu=1/0") #, metavar="FILE")
  38. parser.add_option("", "--out-idim", dest="out_idim",default="",
  39. help=" out_idim") #, metavar="FILE")
  40. parser.add_option("", "--countdown", dest="countdown",#default=1,
  41. help="enable countdown") #, metavar="FILE")
  42. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  43. help="enable videoplayer") #, metavar="FILE")
  44. parser.add_option("", "--title", dest="title",default="SCREEN",
  45. help="set title") #, metavar="FILE")
  46. (options, args) = parser.parse_args()
  47. import numpy
  48. QUIT = 0
  49. for o in dir(options):
  50. if "_" in o:
  51. continue
  52. print(o,options.__getattribute__(o))
  53. START = time.time()
  54. # ===== ARTNET DMX =========
  55. import memcache
  56. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  57. def read_index():
  58. ips=mc.get("index")#cmd)
  59. if ips is None:
  60. ips = {}
  61. return ips
  62. def select_ip(ips, univ=2): # artnet univ
  63. _univ = ":{}".format(univ)
  64. for ip in ips: #high priority
  65. if "2.0.0" in ip and _univ in ip:
  66. return ip
  67. for ip in ips:
  68. if "ltp-out" in ip and _univ in ip:
  69. return ip
  70. INPUT_START_POS_X = 40
  71. INPUT_START_POS_Y = 236 #320
  72. FUNC = 0
  73. COUNTER = []
  74. if options.countdown:
  75. cdmx_start = options.countdown.split(",")
  76. for cdmx in cdmx_start:
  77. try:
  78. cdmx = int(cdmx)
  79. _tmp={"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  80. ,"CONTROL":0,"SEC":10
  81. ,"RED":255,"GREEN":255,"BLUE":255
  82. ,"_time":time.time(),"_RUN":0,"_pause":0,"_start":0
  83. ,"_SEC":">{}<".format(cdmx)
  84. }
  85. COUNTER.append(_tmp)
  86. except Exception as e:
  87. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  88. def read_dmx(ip):
  89. r = ""
  90. if ip:
  91. #t = int(math.sin(time.time() - s)*10)
  92. r = mc.get(ip) #"2.0.0.13:2")
  93. rr = [0]*512
  94. for i,v in enumerate(r):
  95. try: #cleanup ltp-out to int
  96. v = int(v)
  97. rr[i] = v
  98. except:pass
  99. r = rr
  100. if not r:
  101. c = 0
  102. #time.sleep(0.1)
  103. r = [0] *512
  104. for i in range(12*8+1):
  105. dmx = i*4
  106. #print(dmx)
  107. r[dmx:dmx+4] = [255,10,10,40]
  108. return r
  109. # ===== ARTNET DMX =========
  110. MEDIALIST = []
  111. try:
  112. MEDIALIST = open_medialist()
  113. except:pass
  114. from tool.video_capture import scan_capture #as scan_capture
  115. def _rescale_frame_by_width(frame, width):
  116. #print(type(frame)) #np array
  117. height = int(frame.shape[0]/frame.shape[1] * width )
  118. dim = (width, height)
  119. #return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  120. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  121. def _rescale_frame_by_procent(frame, percent=75):
  122. #print(type(frame))
  123. width = int(frame.shape[1] * percent/ 100)
  124. height = int(frame.shape[0] * percent/ 100)
  125. dim = (width, height)
  126. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  127. def _moveImage(img,x=0,y=0,shape=None):
  128. #print(type(img)) #np.array
  129. # Creating a translation matrix
  130. np = numpy
  131. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  132. if shape is None:
  133. num_cols,num_rows = img.shape[1::-1]
  134. else:
  135. num_cols,num_rows = shape #[1::-1]
  136. # Image translation
  137. frame = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  138. return frame
  139. def _rotateImage(image, angle):
  140. #print(type(image)) # nm.array
  141. if angle in [0,360]:
  142. pass#return image
  143. try:
  144. #print("EE",image.shape)
  145. #print("EE",dir(image))
  146. shape = list(image.shape[1::-1])
  147. bg_shape = shape[:]
  148. delta_x = 0 # + nach rechts
  149. delta_y = 0 # + nach unten
  150. r = []
  151. r.extend(list(range(0-3,0+3)))
  152. r.extend(list(range(180-3,180+3)))
  153. r.extend(list(range(360-3,360+3)))
  154. r2 = []
  155. r2.extend(list(range(90-3,90+3)))
  156. r2.extend(list(range(270-3,270+3)))
  157. if angle in r:
  158. pass
  159. elif 0:#angle in r2:
  160. bg_shape = bg_shape[::-1]
  161. delta_y = -65 #shape[1]/2 # # + nach unten
  162. delta_x = 30 #shape[1]/2 # # + nach unten
  163. delta_y -= bg_shape[1]/2 - 20
  164. delta_x = bg_shape[0]/2 - 10
  165. pass
  166. else:
  167. if shape[0] > shape[1]:
  168. # landscape video
  169. bg_shape = (shape[0],shape[0])
  170. delta_y = (shape[0]-shape[1])/2
  171. else:
  172. # portrait video
  173. bg_shape = (shape[1],shape[1])
  174. delta_x = (shape[1]-shape[0])/2
  175. center = (shape[0]/2+delta_x,shape[1]/2+delta_y)
  176. image = _moveImage(image,x=delta_x,y=delta_y,shape=bg_shape)
  177. bg_shape = tuple(bg_shape) # exception ... if angle 0 no tuple !!!
  178. rot_mat = cv2.getRotationMatrix2D(center,angle,1.0)
  179. frame = cv2.warpAffine(image, rot_mat, bg_shape ) #,flags=cv2.INTER_LINEAR)
  180. return frame
  181. except Exception as e:
  182. raise(e)
  183. def calc_dim(dim=1,rgb=[255,255,255]):
  184. rgb[0] = int(rgb[0]*dim/255)
  185. rgb[1] = int(rgb[1]*dim/255)
  186. rgb[2] = int(rgb[2]*dim/255)
  187. return rgb
  188. def draw_media_background(wn,x,y,w,h,rgb):
  189. hx=int(w/2)
  190. hy=int(h/2)
  191. p1 = [x-hx ,y-hy] # links oben
  192. p2 = [x+hx ,y+hy] # recht unten
  193. p3 = [x+hx ,y-hy] # rechts oben
  194. p4 = [x-hx ,y+hy] # links unten
  195. bg_on=0
  196. if bg_on:
  197. #pygame.draw.rect(wn,rgb ,[p1[0] ,p1[1] ,__xw ,__yw])
  198. #pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,hx-2,hx-2])
  199. pass
  200. # frame border
  201. pygame.draw.line(wn,rgb ,p1 ,p3) # top
  202. pygame.draw.line(wn,rgb ,p2 ,p3) # right
  203. pygame.draw.line(wn,rgb ,p2 ,p4) # bottom
  204. pygame.draw.line(wn,rgb ,p1 ,p4) # right
  205. # cross lines
  206. pygame.draw.line(wn,rgb ,p1 ,p2) # tl - br
  207. pygame.draw.line(wn,rgb ,p3 ,p4) # tr - bl
  208. class MediaImg():
  209. def __init__(self):
  210. pass
  211. class MediaCam():
  212. def __init__(self):
  213. pass
  214. def draw(self,wn,im,x,y):
  215. w2 = int(im.get_width()/2)
  216. h2 = int(im.get_height()/2)
  217. wn.blit(im, (int(x-w2), int(y-h2))) # draw frame
  218. def _open_cam(self,obj):
  219. # LIVE VIDEO INPUT UVC DEVICE
  220. cfg = self._cam_cfg()
  221. print("III",cfg)
  222. res = "HD"
  223. #res = "4K"
  224. #res = ""
  225. if res == "HD":
  226. # HD
  227. w = 1920
  228. h = 1080
  229. elif res == "4K":
  230. # 4K
  231. w = 3840
  232. h = 2160
  233. else:
  234. # LOW
  235. w = 1280
  236. h = 720
  237. d = 0 # VIDEO INPUT DEV
  238. for c in cfg:
  239. try:
  240. if c.startswith("d"):
  241. d = int(c[1:])
  242. if c.startswith("h"):
  243. h = int(c[1:])
  244. if c.startswith("w"):
  245. w = int(c[1:])
  246. if c.startswith("video"):
  247. d = int(c[-1])
  248. except Exception as e:
  249. cprint("Exception", "cam_cfg 878",e,color="red")
  250. df = "video{}".format(d)
  251. if obj._stop:
  252. df += "_stop"
  253. elif os.path.exists("/dev/{}".format(df)):
  254. obj.Rcap = cv2.VideoCapture(d)
  255. obj.Rcap.set(cv2.CAP_PROP_FRAME_WIDTH, w)
  256. obj.Rcap.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
  257. else:
  258. #obj.Rcap = None
  259. #obj.end = 1
  260. df += "_error"
  261. if obj.fname.startswith("cam_"):
  262. obj.fname = "cam_{}".format(df)
  263. def _cam_cfg(self):
  264. fn = HOME+'/LibreLight/video_in.txt'
  265. if not os.path.isfile(fn):
  266. self._cam_cfg_new(fn)
  267. f = open(fn ,"r")
  268. lines = f.readlines()
  269. f.close()
  270. cfg = ""
  271. for i in lines:
  272. if i.startswith("d"):
  273. cfg = i
  274. cfg = cfg.split()
  275. capture = scan_capture(name="MiraBox",serial="")
  276. #capture = scan_capture(name="Integrated",serial="")
  277. if capture:
  278. print("capture",capture)
  279. try:
  280. cfg.append(capture[0][0])
  281. except Exception as e:
  282. cprint("except 2992",e,color="red")
  283. else:
  284. cprint("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----",color="red")
  285. print("_cam_cfg:",len(cfg),[cfg])
  286. return cfg
  287. def _cam_cfg_new(self,fn):
  288. #fn = HOME+'/LibreLight/video_in.txt'
  289. txt = [
  290. "# Example",
  291. "d0 w640 h480 # PAL",
  292. "d0 w800 h600 # SVGA",
  293. "d0 w960 h540 # 540p",
  294. "d0 w1024 h768 # XGA <---",
  295. "d0 w1280 h720 # 720p",
  296. "d0 w1440 h900 # WSXGA+",
  297. "d0 w1600 h900 # WSXGA - Laptop 16:9",
  298. "d0 w1920 h1080 # HD 1080p",
  299. "",
  300. "# config",
  301. "d0 w1024 h768 # XGA",
  302. ]
  303. f = open(fn ,"w")
  304. f.write("\n".join(txt))
  305. f.close()
  306. class PixelMedia():
  307. def __init__(self,dmx=None,_id=None):
  308. global MEDIALIST
  309. self._id = _id
  310. self.fpath = HOME+'/Downloads/'
  311. self.fpath = HOME+'/LibreLight/video/'
  312. self.fname = '' #'bbb_sunflower_480x320.mp4'
  313. #self.fname = 'no-video.mp4'
  314. try:
  315. self.fname = MEDIALIST[0]
  316. except Exception as e:
  317. cprint("Exception set video from MEDIALIST 5543:",e,color="red")
  318. self.cam = MediaCam()
  319. self.img_meta = {}
  320. self.restart_t = time.time()
  321. self.fps = 0
  322. self.scale = 50 #%
  323. self.angle = 0 #%
  324. self.dmx=dmx
  325. if self._id >= 250:
  326. self.fname = "cam_"
  327. self.dim = 0
  328. self.x = 0
  329. self.y = 0
  330. self.init_count = 0
  331. self.cap = None
  332. self.shape = [200,200]
  333. self.img = None
  334. self.success = 1
  335. self._run = 0
  336. self.end = 0
  337. self._media_nr = 0
  338. self._stop = 0 # fix buffer-overflow ... ?? opencv
  339. self.shape_x = 370
  340. self.shape_y = 235
  341. self.dmx_change = 0
  342. self.frame_change = 0
  343. self.init()
  344. def update_dmx(self,x,y,scale,angle,dim):
  345. if int(self.x*100) != int(x*100):
  346. self.x = x
  347. self.dmx_change = 1
  348. if int(self.y*100) != int(y*100):
  349. self.y = y
  350. self.dmx_change = 1
  351. if int(self.scale*100) != int(scale*100):
  352. self.scale = scale
  353. self.dmx_change = 1
  354. if int(self.angle*100) != int(angle*100):
  355. self.angle = angle
  356. self.dmx_change = 1
  357. if int(self.dim*100) != int(dim*100):
  358. self.dim = dim
  359. self.dmx_change = 1
  360. def __repr__(self):
  361. sizeof = int(sys.getsizeof(self.buffer)/8)
  362. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  363. id(self),len(self.buffer),self._run,self.fps,self.scale
  364. ,self.fname,sizeof
  365. )
  366. def restart(self):
  367. print(self,"reset()")
  368. self.pos = 0
  369. self.restart_t = time.time()
  370. def init(self):
  371. print("---- ---- PixelMedia.init()",[self.fname,self._media_nr])
  372. #print(MEDIALIST)
  373. self.time = 0
  374. self.t_delta = 0
  375. self.t_last = time.time()
  376. self.im = None
  377. self.pos = 0
  378. self.buffer = []
  379. self._init()
  380. self.init_count = 1
  381. self.dmx_change = 0
  382. self.frame_change = 0
  383. def select_video(self,dmx_value):
  384. try:
  385. dmx_value = int(dmx_value/10)
  386. if self._media_nr != dmx_value:
  387. self._media_nr = dmx_value
  388. if self._media_nr >= 25:
  389. self.fname = "cam_"
  390. self.init()
  391. elif self._media_nr < len(MEDIALIST):
  392. self.fname = str(MEDIALIST[self._media_nr])
  393. self.init()
  394. #finally:pass
  395. except Exception as e:
  396. cprint("PixelMedia.select_video()",dmx_value,e,color="red")
  397. def _init(self):
  398. print(self)
  399. print("videoplayer.init()",self.fpath,self.fname)
  400. if not os.path.isfile(self.fpath+self.fname):
  401. print()
  402. cprint("video file does not exits !! >",self.fpath,self.fname,color="red")
  403. print()
  404. #exit()
  405. self.Rsuccess = 0
  406. if not cv2:
  407. return
  408. if self._stop:
  409. return
  410. if self.fname.endswith(".jpg"):
  411. self.img_meta = create_img_meta()
  412. if self.img_meta["fname"] == self.fname:
  413. return
  414. self.img_meta["_fpath"] = self.fpath
  415. self.img_meta["fname"] = self.fname
  416. img_open(self.img_meta)
  417. self.buffer = [1] #,2,3]
  418. return # STOP
  419. if self.fname.startswith("cam_"):
  420. self.cam._open_cam(obj=self)
  421. self.img_meta = {}
  422. else:
  423. self.Rcap = cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  424. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  425. self.img_meta = {}
  426. print("_init ?",self.Rcap)
  427. print("open ?" ,self.Rcap.isOpened())
  428. self.Rcap.read()
  429. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  430. self.Rsuccess = 1
  431. self.read()
  432. def _del(self):
  433. if self.img_meta:
  434. return
  435. self._stop = 1
  436. self.buffer = [] #.append(self.img)
  437. self.Rcap.release()
  438. self.Rcap.retrieve()
  439. self.Rcap = None
  440. self.cap = None
  441. del self.Rcap #.release()
  442. gc.collect()
  443. def read(self):
  444. if self.img_meta:
  445. return
  446. success = self.Rsuccess
  447. ok = 0
  448. if success and self.fname:
  449. cap = self.Rcap
  450. #fvs = self.Rfvs
  451. _break = 0
  452. try:
  453. success, img = cap.read()
  454. if not success:
  455. self.Rcap.release()
  456. self.Rcap.retrieve()
  457. self.end = 1
  458. #j = json.dumps(self.buffer) #.append(img)
  459. #f = open("/tmp/buff")
  460. #f.write(j)
  461. #f.close()
  462. return
  463. if self.fps == 0:
  464. self.fps = cap.get(cv2.CAP_PROP_FPS)
  465. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  466. if self.fname.startswith("cam_"):
  467. pass
  468. #img = _rescale_frame_by_width(img, 1200)
  469. else:
  470. img = _rescale_frame_by_width(img, 200)
  471. #img = _rescale_frame_by_width(img, 1200)
  472. # store frame into buffer list
  473. if self.fname.startswith("cam_"):
  474. self.buffer = [ img ]
  475. else:
  476. self.buffer.append(img)
  477. ok = 1
  478. if len(self.buffer) % 100 == 0:
  479. _id = str(self.__repr__)[-5:-1]
  480. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  481. except Exception as e:
  482. cprint("Exception","_init",self,e,end="",color="red")
  483. self.success = 1
  484. return ok
  485. def prev(self):
  486. self.pos -= 1
  487. if self.pos < 0:
  488. self.pos = len(self.buffer)-1
  489. if self.pos >= len(self.buffer):
  490. self.pos = len(self.buffer)-1
  491. self.im = self.buffer[int(self.pos)]
  492. def _rotateImage(self,image, angle):
  493. if self.img_meta:
  494. return
  495. return _rotateImage(image, angle)
  496. def pause(self):
  497. if self.img_meta:
  498. return
  499. #print("pause",self.t_last)
  500. t = time.time()
  501. self.t_delta = 0
  502. self.t_last = t
  503. def next_frame(self):
  504. if self.img_meta:
  505. return
  506. #return 0
  507. if self._run and len(self.buffer) > 30:
  508. t = time.time()
  509. self.t_delta = t-self.t_last
  510. self.pos += int(self.t_delta*self.fps)
  511. self.t_last = t
  512. #self.frame_change = 1
  513. else:
  514. self.pause()
  515. if self.restart_t > time.time()-0.5:
  516. # prevent flickerung by reset
  517. #self.pause()
  518. self.pos = 5
  519. pass
  520. # restart at the end
  521. if int(self.pos) >= len(self.buffer)-1:
  522. self.pos = 0
  523. self.img = None
  524. if self.buffer:
  525. self._img = self.buffer[int(self.pos)]
  526. self.img = self._img
  527. def check_cv2_check(self):
  528. if not cv2:
  529. cprint("cv2 not importet ! no VIDEO support !",color="red")
  530. return 0
  531. return 1
  532. def next(self):
  533. if self.img_meta:
  534. self.next_frame()
  535. return
  536. try:
  537. self.next_frame()
  538. if self.img is None:
  539. return
  540. self.img = _rescale_frame_by_procent(self.img, percent=self.scale)
  541. # rotate frame BUG: x,y offset ??? !!!
  542. self.img = self._rotateImage(self.img, self.angle)
  543. self.shape = self.img.shape[1::-1]
  544. if len(self.buffer) % 100 == 0:
  545. _id = str(self.__repr__)[-5:-1]
  546. print("next",_id)
  547. # add DIMER to videplayer
  548. cv2.normalize(self.img, self.img, 0, self.dim, cv2.NORM_MINMAX)
  549. img = self.img
  550. if img is None:
  551. return
  552. self.im = None
  553. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  554. #self.next_frame()
  555. except AttributeError as e:
  556. time.sleep(.05)
  557. #if self.init_count % 100 == 0:
  558. cprint("except 776",e,color="red")
  559. #self.init()
  560. except Exception as e:
  561. cprint("except 756",e,color="red")
  562. cprint(traceback.format_exc(),color="red")
  563. cprint(sys.exc_info()[2],color="red")
  564. cprint()
  565. def calc_im_size(self):
  566. try:
  567. xx ,yy = self.im.get_size()[:2]
  568. self.shape_x = xx
  569. self.shape_y = yy
  570. except AttributeError as e:
  571. pass
  572. def calc_zoom(self,xx,yy):
  573. if self.scale < 255*.05:
  574. self.scale = 255*0.05
  575. xx = int(xx*self.scale/255)
  576. yy = int(yy*self.scale/255)
  577. return xx,yy
  578. def draw_video(self,wn,im,x,y):
  579. w2 = int(im.get_width()/2)
  580. h2 = int(im.get_height()/2)
  581. wn.blit(im, (int(x-w2), int(y-h2))) # draw frame
  582. def draw_img(self,wn,img_meta):
  583. self.img_meta["pos"] = [self.x,self.y]
  584. self.img_meta["scale"] = self.scale #= 50 #%
  585. self.img_meta["dim"] = self.dim #255
  586. self.img_meta["ang"] = self.angle # 0
  587. if 1: #self.is_change():
  588. im2 = self.img_meta["raw_img"]
  589. im2 = img_rescale(im2, percent=self.img_meta["scale"])
  590. # add DIMER to videplayer
  591. im2 = cv2.normalize(im2, im2, 0, self.img_meta["dim"], cv2.NORM_MINMAX)
  592. self.img_meta["img"] = im2
  593. img_draw_raw(self.img_meta)
  594. self.change_reset()
  595. def is_change(self):
  596. #if self.frame_change or self.dmx_change:
  597. if self.frame_change or self.dmx_change:
  598. #if self.dmx_change:
  599. return True
  600. def change_reset(self):
  601. if self.frame_change:
  602. print("media frame change")
  603. if self.dmx_change:
  604. print("media dmx change")
  605. self.frame_change = 0
  606. self.dmx_change = 0
  607. def draw(self,wn=None):
  608. if self.img_meta:
  609. self.draw_img(wn,self.img_meta)
  610. return
  611. if self.dim <= 1:
  612. return
  613. self.calc_im_size()
  614. im = self.im
  615. x=self.x
  616. y=self.y
  617. #w,h = self.calc_zoom(x,y)
  618. w = self.shape_x
  619. h = self.shape_y
  620. if self.success and wn and im:
  621. if self.fname.startswith("cam_"):
  622. self.cam.draw(wn,im,x,y)
  623. else:
  624. self.draw_video(wn,im,x,y)
  625. self.change_reset()
  626. def get_meta(self):
  627. out = {}
  628. mode="x"
  629. if self._run:
  630. mode = "run"
  631. else:
  632. mode = "pause"
  633. out["frames"] = len(self.buffer)
  634. out["mode"] = mode
  635. out["fname"] = self.fname
  636. out["scale"] =self.scale
  637. out["fps"] = int(self.fps)
  638. out["pos"] = self.pos
  639. out["dim"] = self.dim
  640. out["run"] = self._run
  641. out["cur"] = 0
  642. if self.fps > 0: # check if div zerro
  643. out["cur"] = (self.pos/self.fps)
  644. out["x"] = self.x
  645. out["y"] = self.y
  646. out["id"] = self._id
  647. out["nr"] = self._media_nr
  648. out["pz"] = 0
  649. return out
  650. def _overlay_text(self):
  651. meta = self.get_meta()
  652. keys = ["fps","pos","frames","cur","pz","x","y","scale","dim"]
  653. _line = create_format_line(meta,keys)
  654. keys= ["id","nr","mode","fname"]
  655. _line2 = create_format_line(meta,keys)
  656. return [_line,_line2]
  657. def create_format_line(meta,keys):
  658. _line = []
  659. for k in keys:
  660. v=meta.get(k,"-")
  661. _line.append(str(k))
  662. _line.append(":")
  663. if type(v) is float:
  664. _line.append("{:02f}".format(v))
  665. else:
  666. _line.append(str(v))
  667. _line.append(" ")
  668. _line = "".join(_line)
  669. return _line
  670. PixelMedia = PixelMedia
  671. VIDEO = []
  672. videoplayer=[]
  673. cv2 = None
  674. FileVideoStream = None
  675. _vid = 0
  676. if type(options.videoplayer) is str:
  677. try:
  678. import cv2
  679. except Exception as e:
  680. cprint("Except Import:",e,color="red")
  681. try:
  682. # faster video reading ... ???
  683. from imutils.video import FileVideoStream
  684. except Exception as e:
  685. cprint("Except Import:",e,color="red")
  686. max_videoplayer = 4
  687. dmx_start = options.videoplayer.split(",")
  688. univ = 0
  689. for cdmx in dmx_start:
  690. if "." in cdmx:
  691. univ,cdmx=cdmx.split(".",1)
  692. if len(videoplayer) > max_videoplayer:
  693. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  694. break
  695. print("-- videoplayer dmx:",cdmx)
  696. try:
  697. cdmx = int(cdmx)
  698. univ = int(univ)
  699. videoplayer.append( PixelMedia(cdmx,_id=_vid) )
  700. _vid += 1
  701. _tmp = {"DMX":cdmx,"UNIV":univ,"DIM":0,"PAN":127,"TILT":127
  702. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  703. ,"RED":255,"GREEN":255,"BLUE":255
  704. ,"_time":time.time()
  705. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  706. }
  707. VIDEO.append(_tmp)
  708. except Exception as e:
  709. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  710. def loop_videoplayer():
  711. while 1:
  712. _videoplayer = videoplayer[:]
  713. #print(".")
  714. ok = 0
  715. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  716. try:
  717. r = i.read() # read next frame from file
  718. if r:
  719. ok = 1
  720. except Exception as e:
  721. cprint("EXCEPTION loop_videoplayer ",e,color="red")
  722. cprint(traceback.format_exc(),color="red")
  723. cprint(sys.exc_info()[2],color="red")
  724. cprint()
  725. if ok == 0:
  726. time.sleep(0.1)
  727. else:
  728. time.sleep(0.005)
  729. if QUIT:
  730. break
  731. thread.start_new_thread(loop_videoplayer,())
  732. p = 16
  733. block = [p,p]
  734. _x = 8
  735. _y = 8
  736. #HD = "0"
  737. if options.mode:
  738. try:
  739. HD = options.mode
  740. p,_x,_y = HD.split(",")
  741. _x = int(_x)
  742. _y = int(_y)
  743. p = int(p)
  744. block = [p,p]
  745. except Exception as e:
  746. cprint( "Exc",options.mode,e,color="red")
  747. # PARSE COMMANDLINE ARGUMENTS
  748. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":0,"y2":0 ,"w":0,"h":300}
  749. # OUTPUT CFG INIT
  750. CFG_OUT = []
  751. out_idim = options.out_idim
  752. if out_idim:
  753. out_idim = options.out_idim.split(",")
  754. x1 = 40
  755. for i,v in enumerate(out_idim):
  756. print("--out-idim",[i,v])
  757. _t={}
  758. try:
  759. v = int(v)
  760. _t = {"name":"CFG_OUT{}".format(i+1),"x1":x1,"y1":60,"x2":0,"y2":0 ,"w":30,"h":30,"on":1,"DMX-CH":v}
  761. print(_t,v)
  762. CFG_OUT.append(_t)
  763. x1 += p*8
  764. except Exception as e:
  765. print("err",_t,color="red")
  766. print("err",e,color="red")
  767. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  768. if _x < 1:
  769. _x = 1
  770. if _y < 1:
  771. _y = 1
  772. CFG_BLOCK["h-count"] = _x
  773. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  774. CFG_BLOCK["size"] = p
  775. print( [options.xsplit])
  776. print( [options.ysplit])
  777. try:
  778. if options.xsplit:
  779. CFG_BLOCK["h-split"] = int(options.xsplit)
  780. if options.ysplit:
  781. CFG_BLOCK["v-split"] = int(options.ysplit)
  782. except Exception as e:
  783. cprint( "Exc",options.mode,e,color="red")
  784. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  785. print("xy",_x,_y)
  786. print("++++++++++++++++++", p,_x,_y)
  787. _x2 = _x
  788. try:
  789. if options.XX:
  790. pass#_x2 = int(options.XX)
  791. except Exception as e:
  792. cprint( "Exc",options.mode,e,color="red")
  793. print("_x2 , -X",_x2)
  794. # ===== GUI =========
  795. import pygame
  796. import pygame.gfxdraw
  797. import pygame.font
  798. clock = pygame.time.Clock()
  799. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  800. if options.win_pos:
  801. if "," in options.win_pos:
  802. win_pos = options.win_pos.split(",")
  803. try:
  804. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  805. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  806. except Excetpion as e:
  807. cprint("win_pos",win_pos,e,color="red")
  808. os.environ['SDL_VIDEO_CENTERED'] = '0'
  809. pg = pygame
  810. pygame.init()
  811. pygame.mixer.quit()
  812. f = pygame.font.get_fonts()
  813. for i in f:
  814. if "mono" in i.lower():
  815. print(i)
  816. font = pygame.font.SysFont("freemonobold",22)
  817. font10 = pygame.font.SysFont("freemonobold",10)
  818. font12 = pygame.font.SysFont("freemonobold",12)
  819. font12l = pygame.font.SysFont("freemono",12)
  820. font15 = pygame.font.SysFont("freemonobold",15)
  821. font40 = pygame.font.SysFont("freemonobold",40)
  822. font80 = pygame.font.SysFont("freemonobold",70)
  823. #font = pygame.font.SysFont(None,30)
  824. fr = font.render("hallo" ,1, (200,0,255))
  825. def clean_path(path):
  826. _path = path[:]
  827. _path = _path.replace("/","-")
  828. _path = _path.replace(".","-")
  829. _path = _path.replace("\"","-")
  830. _path = _path.replace("'","-")
  831. return _path
  832. PIXEL_MAPPING = 0
  833. vpu_wall_conf_file = HOME+"/LibreLight/vpu_wall_conf.csv"
  834. grid_file = "/tmp/vpu_grid_hd.csv"
  835. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  836. media_list = HOME+"/LibreLight/video/" #.format(path)
  837. pm_wy = 0
  838. if options.pixel_mapping:
  839. PIXEL_MAPPING = 1
  840. path = options.pixel_mapping
  841. path = clean_path(path)
  842. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  843. if options.dual_vpu:
  844. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  845. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  846. media_list = HOME+"/LibreLight/video/" #.format(path)
  847. if not options.out_idim:
  848. PIXEL_MAPPING = 0
  849. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  850. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  851. MAIN_SIZE=(600,500)
  852. try:
  853. if _x < 8 and PIXEL_MAPPING >= 1:
  854. wx = 30+30+block[0] * 8
  855. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  856. else:
  857. wx = 30+30+block[0] * _x
  858. wy = 40+40+block[1] * _y
  859. if type(options.videoplayer) is str:
  860. wy += 150 # video medialist
  861. MAIN_SIZE=(wx,wy)
  862. if PIXEL_MAPPING >= 1:
  863. pm_wy = 11*p #+ p*3
  864. CFG_IN["y1"] += 11*p
  865. if options.dual_vpu:
  866. if wx < p*16+155:
  867. wx = p*16+155
  868. MAIN_SIZE=(wx,wy+pm_wy)
  869. except Exception as e:
  870. cprint("Exception:",e,color="red")
  871. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  872. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  873. for CFG_OUTx in CFG_OUT:
  874. CFG_OUTx["w"] = CFG_BLOCK["size"] * 8
  875. CFG_OUTx["h"] = CFG_BLOCK["size"] * 8
  876. def CFG_CALC_P(CFG):
  877. CFG["x2"] = CFG["x1"]+CFG["w"]
  878. CFG["y2"] = CFG["y1"]+CFG["h"]
  879. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  880. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  881. print("CFG",CFG)
  882. # ?
  883. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  884. CFG_CALC_P(CFG_IN)
  885. for CFG_OUTx in CFG_OUT:
  886. CFG_CALC_P(CFG_OUTx)
  887. print("CFG_BLOCK",CFG_BLOCK)
  888. print()
  889. print()
  890. print()
  891. print()
  892. print()
  893. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  894. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  895. class Fix():
  896. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  897. #print("Fix",_id)
  898. self._id = _id
  899. self.dmx = (_id-1) * ch +1 #dmx
  900. self.univ = univ
  901. self.ch = ch
  902. self.pos = pos
  903. self.rgb = [0,0,0]
  904. self.block = block #[10,10]
  905. self.x = pos[0]
  906. self.y = pos[1]
  907. self._x = 0
  908. self._y = 0
  909. self.strobo = time.time()
  910. self.bmp = 250
  911. self.sub_fix = []
  912. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  913. if _id <= 0: #exit
  914. return
  915. spalte = (_id-1)%_y +1
  916. zeile = int((_id-1)/_x2) #+1
  917. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  918. #zeile 1
  919. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  920. sid = sid+1
  921. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  922. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  923. self.sub_fix.append(sub_fix)
  924. sid = sid+1
  925. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  926. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  927. self.sub_fix.append(sub_fix)
  928. #zeile 2
  929. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  930. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  931. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  932. self.sub_fix.append(sub_fix)
  933. #sid = sid+1
  934. sid = sid+1
  935. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  936. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  937. self.sub_fix.append(sub_fix)
  938. def __repr__(self):
  939. return "<Fix: {} {} {} {}>".format(id(self),self.x,self.y,self.dmx)
  940. def calc(self,data):
  941. _rgb = [0,255,0]
  942. return _rgb
  943. def sub_calc(self,data):
  944. _rgb = [0,255,0]
  945. for sub_fix in self.sub_fix:
  946. sub_fix.block = self.block[:]
  947. _rgb = sub_fix.dmx_calc(data)
  948. return _rgb
  949. def _calc(self,a,b):
  950. A = (self.pos[0])*self.block[0]
  951. B = (self.pos[1])*self.block[1]
  952. C = self.block[0]-a
  953. D = self.block[1]-b
  954. return A,B,C,D
  955. def POS(self,x=None,y=None,a=0,b=0):
  956. if type(x) is int:
  957. self._x=x
  958. if type(y) is int:
  959. self._y=y
  960. A,B,C,D = self._calc(a,b)
  961. return [self._x+A,self._y+B,C,D]
  962. def XXXsubPOS(self,x=0,y=0,a=0,b=0):
  963. __out = []
  964. for sub_fix in self.sub_fix:
  965. __out.append( sub_fix.POS(x,y,a,b) )
  966. return __out
  967. class SubFix():
  968. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  969. #print("Fix",_id)
  970. self._id = _id
  971. self.dmx = (_id-1) * ch +1 #dmx
  972. self.univ = univ
  973. self.ch = ch
  974. self.pos = pos
  975. self.rgb = [0,0,40]
  976. self.block = block #[10,10]
  977. self.x = pos[0]
  978. self.y = pos[1]
  979. self._x = 0
  980. self._y = 0
  981. self.strobo = time.time()
  982. self.bmp = 250
  983. def dmx_calc(self,data):
  984. #return [130,30,20]
  985. dmx_sub = [30]*10
  986. dmx = self.dmx -1
  987. _dmx_sub = []
  988. if self.dmx >= 0:
  989. dmx = rDMX(self.univ,self.dmx)-1
  990. if dmx+self.ch < len(data):
  991. _dmx_sub = data[dmx:dmx+self.ch]
  992. if _dmx_sub:
  993. dmx_sub = _dmx_sub
  994. dim = dmx_sub[0]/255
  995. r = dmx_sub[1]*dim
  996. g = dmx_sub[2]*dim
  997. b = dmx_sub[3]*dim
  998. r = int(r)
  999. g = int(g)
  1000. b = int(b)
  1001. self.rgb = [r,g,b]
  1002. return self.rgb
  1003. def _calc(self,a,b):
  1004. A = (self.pos[0]) #+self.block[0]
  1005. B = (self.pos[1]) #+self.block[1]
  1006. C = self.block[0]-a
  1007. D = self.block[1]-b
  1008. return A,B,C,D
  1009. def POS(self,x=None,y=None,a=0,b=0):
  1010. if type(x) is int:
  1011. self._x=x
  1012. if type(y) is int:
  1013. self._y=y
  1014. A,B,C,D = self._calc(a,b)
  1015. #if NR:
  1016. # C-=1
  1017. # D-=1
  1018. return [int(self._x+A),int(self._y+B),int(C),int(D)]
  1019. class POINTER():
  1020. def __init__(self):
  1021. self.pos = [0,0,0,0]
  1022. self.on = 0
  1023. self.rgb = [0,100,10]
  1024. self._x = 0
  1025. self._y = 0
  1026. self.x = 0
  1027. self.y = 0
  1028. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  1029. def row_move(self,x,y):
  1030. self._x = x
  1031. self._y = y
  1032. def move(self,pos):
  1033. self.pos = pos
  1034. self.on = 1
  1035. def cross(self,x,y):
  1036. self.x = x
  1037. self.y = y
  1038. def draw(self,x,y):
  1039. pos = self.pos[:]
  1040. #print("draw",x,y,pos)
  1041. pos[0] += x
  1042. pos[1] += y
  1043. fix_x= self.fix.x
  1044. fix_y= self.fix.y +y
  1045. if 0:# self.on:
  1046. pygame.draw.rect(window,self.rgb,pos)
  1047. # mouse grid posision
  1048. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  1049. _nr = fix_y * _x + fix_x +1
  1050. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1051. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1052. window.blit(fr,(130,1))
  1053. if 1:# lupe / looking-glass / lens
  1054. __t = 60
  1055. f_x = CFG_IN["y2"]+5
  1056. rgb=[255,0,0]
  1057. pos=[400,400,160,160]
  1058. pos=[400,f_x,160,160]
  1059. #pos=[400,400,f_x,f_x]
  1060. draw_box_frame(window,rgb,pos,offset=1)
  1061. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1062. if r_sub:
  1063. r_sub = pygame.transform.scale(r_sub,(160,160))
  1064. if r_sub: # target
  1065. window.blit(r_sub, (400,f_x))
  1066. #pos=[400+40,400+40,80,80]
  1067. pos=[400+80,400+80,80,80]
  1068. pos=[400+80,f_x+80,80,80]
  1069. draw_box_frame(window,rgb,pos,offset=3)
  1070. draw_box_frame(window,rgb,pos,offset=2)
  1071. draw_box_frame(window,rgb,pos,offset=0)
  1072. draw_box_frame(window,rgb,pos,offset=1)
  1073. # fix pos
  1074. #txt=str(pos) #"[0, 0, 0, 0]"
  1075. #fr = font15.render(txt ,1, (200,200,200))
  1076. #window.blit(fr,(10,1))
  1077. # pointer
  1078. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1079. window.blit(fr,(10,30))
  1080. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1081. window.blit(fr,(10,40))
  1082. if 1: #self._x > 30 and self._y > 230:
  1083. # pointer
  1084. _x = self._x-INPUT_START_POS_X
  1085. _y = self._y-INPUT_START_POS_Y
  1086. rgb = [200,200,200]
  1087. if _x < 0:
  1088. rgb = [200,0,0]
  1089. fr = font40.render("X:{:03}".format(_x) ,1, rgb)
  1090. f_x = CFG_IN["y2"]+8
  1091. window.blit(fr,(100,f_x))
  1092. rgb = [200,200,200]
  1093. if _y < 0:
  1094. rgb = [200,0,0]
  1095. fr = font40.render("Y:{:03}".format(_y) ,1, rgb)
  1096. window.blit(fr,(200,f_x))
  1097. p=110
  1098. # crosshair
  1099. self.rgb = [0,0,200]
  1100. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1101. #pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1102. self.rgb = [0,100,200]
  1103. pygame.draw.line(window,self.rgb, (self.x,self.y-p/3) , (self.x,self.y-2),2 )
  1104. self.rgb = [0,200,0]
  1105. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1106. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1107. self.rgb = [200,0,0]
  1108. pointer = POINTER()
  1109. NR = 0
  1110. running = True
  1111. def event():
  1112. global NR,running
  1113. for event in pygame.event.get():
  1114. #print(event.dict)
  1115. _button = None
  1116. if "button" in event.dict:
  1117. _button = event.dict["button"]
  1118. _state = None
  1119. if "state" in event.dict:
  1120. _state = event.state
  1121. _key = None
  1122. if "key" in event.dict:
  1123. _key = event.key
  1124. _pos = None
  1125. if "pos" in event.dict:
  1126. _pos = event.pos
  1127. _type = None
  1128. if "type" in event.dict:
  1129. _type = event.type
  1130. _type = event.type
  1131. _mod = None
  1132. if "mod" in event.dict:
  1133. _mod = event.mod
  1134. if 0:
  1135. print( " ")
  1136. print( "{:.02f}".format( time.time() - START ))
  1137. print("button -",_button,end="\t| ")
  1138. #print("state -",_state)
  1139. print("pos -",_pos)
  1140. print("type -",_type, end="\t| ")
  1141. print("key -",_key)
  1142. print("mod -",_mod)
  1143. try:
  1144. if _type in [5,1025]:
  1145. if _button == 1:
  1146. NR += 1
  1147. if NR > 1:
  1148. NR = 0
  1149. if _button == 3:
  1150. NR -= 1
  1151. if NR < 0:
  1152. NR = 1
  1153. if _pos:
  1154. posA = _pos
  1155. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1156. if fix:
  1157. #pos = fix.POS(40,60+pm_wy)
  1158. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1159. rgb = [0,0,0]
  1160. pointer.move(pos)
  1161. pointer.fix = fix
  1162. else:
  1163. pointer.on = 0
  1164. pointer.row_move(_pos[0],_pos[1])
  1165. pointer.cross(_pos[0],_pos[1])
  1166. if event.type == pygame.VIDEORESIZE:
  1167. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1168. except Exception as e:
  1169. cprint(e,color="red")
  1170. if event.type==pygame.QUIT:
  1171. global QUIT
  1172. QUIT = 1
  1173. cprint("QUIT",color="red")
  1174. running=False
  1175. #time.sleep(1)
  1176. #sys.exit()
  1177. fps = 0
  1178. fps2 = 0
  1179. frame = 0
  1180. frame2 = 0
  1181. frame_t = time.time()
  1182. frame2_t = time.time()
  1183. IP = "yyy"
  1184. vplay=[]
  1185. vplay1=0
  1186. vplay2=0
  1187. if options.videoplayer:
  1188. try:
  1189. t=options.videoplayer.split(",")
  1190. vplay=t
  1191. vplay1=t[0]
  1192. vplay2=t[1]
  1193. except:pass
  1194. def output_mask(): # oben
  1195. RGB = [0,0,50]
  1196. #RGB = [120,0,0] # test
  1197. start = 0
  1198. # oben
  1199. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1200. # unten
  1201. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1202. # links
  1203. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1204. def input_mask(): # unten
  1205. RGB = [0,0,50]
  1206. #RGB = [120,0,110] # test
  1207. start = 227
  1208. #f_x = CFG_IN["x2"]-50
  1209. f_x = CFG_IN["y2"]+3
  1210. # oben
  1211. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1212. # links
  1213. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1214. # unten
  1215. #pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1216. pygame.draw.rect(window,RGB,[0,f_x,MAIN_SIZE[0]+200,198]) # background
  1217. def draw_main_overlay():
  1218. global fps,fps2
  1219. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1220. window.blit(fr,(10,2))
  1221. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1222. window.blit(fr,(10,12))
  1223. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1224. #window.blit(fr,(100,2))
  1225. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1226. window.blit(fr,(90,2))
  1227. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1228. window.blit(fr,(210,2))
  1229. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1230. window.blit(fr,(210,12))
  1231. for i,v in enumerate(vplay):
  1232. fr = font15.render("v-play{}: {}.{:}".format(i+1,START_UNIV_A,v) ,1, (200,0,255))
  1233. window.blit(fr,(300,2+12*i))
  1234. try:
  1235. global pointer
  1236. # pointer
  1237. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1238. window.blit(fr,(10,30))
  1239. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1240. window.blit(fr,(10,40))
  1241. except Exception as e:
  1242. print("Overlay err:",e)
  1243. def draw_marker():
  1244. # kennlinie linker rand
  1245. # limit marker INPUT AREA
  1246. rgb = [0,127,255] # orange
  1247. for CFG_OUTx in CFG_OUT: #["y1"]
  1248. f_x = CFG_OUTx["y1"]
  1249. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1250. f_x = CFG_OUTx["y2"]
  1251. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1252. if CFG_OUT:
  1253. f_x = CFG_OUT[0]["y1"]
  1254. for i,v in enumerate("OUTPUT"):
  1255. fr = font15.render(v ,1, rgb) #(200,0,255))
  1256. if v == "I":
  1257. window.blit(fr,(10+2,f_x+i*15+15))
  1258. else:
  1259. window.blit(fr,(10,f_x+i*15+15))
  1260. # kennlinie linker rand
  1261. # limit marker INPUT AREA
  1262. rgb = [255,127,0] # orange
  1263. f_x = CFG_IN["y1"]
  1264. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1265. f_x = CFG_IN["y2"]
  1266. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1267. f_x = CFG_IN["y1"]
  1268. for i,v in enumerate("INPUT"):
  1269. fr = font15.render(v ,1, rgb) #(200,0,255))
  1270. if v == "I":
  1271. window.blit(fr,(10+2,f_x+i*15+15))
  1272. else:
  1273. window.blit(fr,(10,f_x+i*15+15))
  1274. def draw_box_frame(window,rgb,pos,offset=0):
  1275. p1 = [pos[0],pos[1]]
  1276. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1277. o = offset
  1278. _p1 = [p1[0]-o,p1[1]-o]
  1279. _p2 = [p1[0]-o,p2[1]+o]
  1280. pygame.draw.line(window,rgb,_p1,_p2) # left
  1281. _p1 = [p2[0]+o,p2[1]+o]
  1282. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1283. _p2 = [p2[0]+o,p1[1]-o]
  1284. pygame.draw.line(window,rgb,_p1,_p2) # right
  1285. _p1 = [p1[0]-o,p1[1]-o]
  1286. pygame.draw.line(window,rgb,_p1,_p2) # top
  1287. def draw_frame(window,rgb,p1,p2,offset=0):
  1288. o = offset
  1289. _p1 = [p1[0]-o,p1[1]-o]
  1290. _p2 = [p1[0]-o,p2[1]+o]
  1291. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1292. _p1 = [p2[0]+o,p2[1]+o]
  1293. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1294. _p2 = [p2[0]+o,p1[1]-o]
  1295. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1296. _p1 = [p1[0]-o,p1[1]-o]
  1297. pygame.draw.line(window,rgb,_p1,_p2) # top
  1298. def calc_fps():
  1299. global fps,frame,frame_t
  1300. t = time.time()
  1301. if frame_t+1 < t:
  1302. fps = frame #frame_t- t #frame
  1303. frame = 1
  1304. frame_t = time.time()
  1305. def calc_fps2():
  1306. global fps2,frame2,frame2_t
  1307. t = time.time()
  1308. if frame2_t+0.1 < t:
  1309. fps2 = frame2*10 #frame_t- t #frame
  1310. frame2 = 1
  1311. frame2_t = t #time.time()
  1312. TEXT_BLOCK = []
  1313. def _create_text_block():
  1314. print("======== CREATE NEW TEXT FILE !!",text_file)
  1315. f = open(text_file,"w")
  1316. for i in range(10):
  1317. f.write("TEXT {}\n".format(i+1))
  1318. f.close()
  1319. def open_text_block():
  1320. print("======== OPEN TEXT FILE !!",text_file)
  1321. _lines = []
  1322. try:
  1323. f = open(text_file,"r")
  1324. _lines = f.readlines()
  1325. f.close()
  1326. except FileNotFoundError as e:
  1327. print("TEXT",e)
  1328. _create_text_block()
  1329. if len(_lines) <= 0:
  1330. _create_text_block()
  1331. lines = []
  1332. for l in _lines:
  1333. #print(">> ",l.strip())
  1334. lines.append(l.strip())
  1335. if len(lines) <= 10:
  1336. for i in range(10-len(lines)):
  1337. lines.append("LINE ERROR")
  1338. return lines
  1339. TEXT_BLOCK = open_text_block()
  1340. TEXT_BLOCK_TIME = time.time()
  1341. # video medialist
  1342. MEDIALIST = []
  1343. def open_medialist():
  1344. print("======== OPEN MEDIALIST DIR !!",media_list)
  1345. if not os.path.isdir(media_list):
  1346. os.system("mkdir -p {}".format(media_list))
  1347. _lines = os.listdir(media_list)
  1348. _lines.sort()
  1349. lines = ['']*25
  1350. i=0
  1351. for l in _lines:
  1352. l = l.strip()
  1353. if "_" in l:
  1354. ll = l.split("_",1)
  1355. #print(">> ",ll)
  1356. try:
  1357. lll = int(ll[0])
  1358. lines[lll] = l
  1359. except:pass
  1360. if len(lines) <= 10:
  1361. for i in range(10-len(lines)):
  1362. lines.append("")#"LINE ERROR")
  1363. return lines
  1364. MEDIALIST_TIME = time.time()
  1365. MEDIALIST = open_medialist()
  1366. # ===== GUI =========
  1367. def draw_circle(surface,color, pos, radius):
  1368. x,y=pos
  1369. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1370. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1371. def rDMX(univ,dmx):
  1372. return univ*512+dmx
  1373. def generate_grid(mapping=0):
  1374. _log = []
  1375. #if PIXEL_MAPPING:
  1376. # log = open(grid_file,"w")
  1377. head = "i,univ,dmx,x,y,ch\n"
  1378. head = "i,univ,dmx,ch\n"
  1379. head = "univ,dmx,x,y,ch\n"
  1380. head = "nr,id,info\n"
  1381. print("csv:",head)
  1382. _log.append(head)
  1383. dmx = 1-1
  1384. ch = 4
  1385. y=0
  1386. x=0
  1387. for i in range((_y)*(_x)):
  1388. #if x > _x and i%_x == 0:
  1389. if x > 8 and i%8 == 0:
  1390. #print("--> -->")
  1391. x=0
  1392. y+=1
  1393. _univ = int(dmx/512)
  1394. _dmx = dmx - (_univ)*512
  1395. pos=[x,y]
  1396. line="{},{},x\n".format(i+1,i+1)
  1397. _log.append(line)
  1398. dmx += ch
  1399. x+=1
  1400. if mapping and PIXEL_MAPPING:
  1401. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1402. log = open(grid_file,"w")
  1403. log.writelines(_log)
  1404. log.close()
  1405. return _log[:] #GRID
  1406. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1407. if mapping and PIXEL_MAPPING:
  1408. try:
  1409. log = open(grid_file,"r")
  1410. except:
  1411. generate_grid(mapping=mapping)
  1412. log = open(grid_file,"r")
  1413. lines = log.readlines()
  1414. else:
  1415. lines = generate_grid()
  1416. GRID = []
  1417. y=0
  1418. x=0
  1419. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1420. for i,line in enumerate(lines[1+start:]): #exclude first line
  1421. #print("rcsv",[line])
  1422. line = line.strip()
  1423. line = line.split(",") # csv
  1424. if i >= _x and i%_x == 0:
  1425. x=0
  1426. y+=1
  1427. if y >= _y:
  1428. break
  1429. _id = int(line[1])
  1430. pos = [x+_xp,y+_yp]
  1431. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1432. GRID.append(f)
  1433. x+=1
  1434. return GRID
  1435. def find_pix(x,y):
  1436. global GRID
  1437. for fix in GRID:
  1438. X = 0
  1439. Y = 0
  1440. pos = fix.POS()
  1441. if x > pos[0] and x < pos[0]+pos[2]:
  1442. X = 1
  1443. if y > pos[1] and y < pos[1]+pos[3]:
  1444. Y = 1
  1445. if X and Y:
  1446. return fix
  1447. GRID = []
  1448. GRID_A = []
  1449. NR = 0
  1450. START_UNIV_A=1
  1451. START_UNIV=2
  1452. if options.start_univ:
  1453. try:
  1454. START_UNIV=int(options.start_univ)
  1455. except Exception as e:
  1456. print("Exception START UNIV",e)
  1457. gobo_ch=1
  1458. if options.gobo_ch:
  1459. try:
  1460. gobo_ch=int(options.gobo_ch)
  1461. except Exception as e:
  1462. print("Exception gobo_ch",e)
  1463. if gobo_ch <= 0:
  1464. gobo_ch = 1
  1465. def grab(x=55,y=55,w=60,h=60):
  1466. crop = None
  1467. rect = pygame.Rect(x, y, w, h)
  1468. try:
  1469. sub = window.subsurface(rect)
  1470. crop = pygame.Surface((w,h))
  1471. crop.blit(sub, (0,0))
  1472. except ValueError as e:
  1473. #cprint("grab():",e,x,y,w,h,color="red")
  1474. pass
  1475. return crop
  1476. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1477. out,x,y,ang,row,col,info
  1478. Wall1,0,0,0,1,1,
  1479. Wall1,0,0,0,2,2,
  1480. Wall1,0,0,0,3,3,
  1481. Wall1,0,0,0,4,4,
  1482. Wall1,0,0,0,5,5,
  1483. Wall1,0,0,0,6,6,
  1484. Wall1,0,0,0,7,7,
  1485. ,,,,,,
  1486. Wall2,0,0,0,4,1,----
  1487. Wall2,0,0,0,5,2,
  1488. Wall2,4,4,0,6,3,
  1489. Wall2,0,0,0,7,4,
  1490. Wall2,0,0,0,8,5,
  1491. Wall2,0,0,0,9,6,
  1492. ,,,,,,
  1493. ,,,,,,
  1494. Wall3,64,48,0,12,1,----
  1495. Wall3,64,64,0,12,2,
  1496. Wall3,64,80,0,12,3,
  1497. Wall3,64,96,0,12,4,
  1498. Wall3,64,112,0,12,5,
  1499. Wall3,64,128,0,12,6,"""
  1500. def create_vpu_wall_conf():
  1501. f=open(vpu_wall_conf_file,"w")
  1502. f.write(vpu_wall_txt)
  1503. f.close()
  1504. print("CREATE VPU CONFIG !")
  1505. wall_data = []
  1506. _last_wall_config_time = time.time()
  1507. def read_wall_pix(init=0):
  1508. global _last_wall_config_time
  1509. global wall_data, NR
  1510. grid = []
  1511. #if not NR and init==0:
  1512. # return
  1513. ok = 0
  1514. if init:
  1515. ok = 1
  1516. if NR:
  1517. if time.time()-_last_wall_config_time > 3:
  1518. ok = 1
  1519. else:
  1520. if time.time()-_last_wall_config_time > 10:
  1521. ok = 1
  1522. if not ok:
  1523. return
  1524. print("read_wall_pix",vpu_wall_conf_file)
  1525. _last_wall_config_time = time.time()
  1526. if not os.path.isfile(vpu_wall_conf_file):
  1527. create_vpu_wall_conf()
  1528. f=open(vpu_wall_conf_file,"r")
  1529. lines = f.readlines()
  1530. for i,line in enumerate(lines):
  1531. line = line.strip()
  1532. if not line:
  1533. continue
  1534. line = line.split(",")
  1535. if not line[0]:
  1536. continue
  1537. #print(i,line)
  1538. if len(line) >= 3:
  1539. _l = []
  1540. for i in line:
  1541. try:
  1542. _l.append(int(i))
  1543. except Exception as e:
  1544. _l.append(i)
  1545. grid.append(_l)
  1546. #for i,v in enumerate(grid):
  1547. # print(i,v)
  1548. wall_data = grid
  1549. read_wall_pix(init=1)
  1550. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1551. global W_ANG
  1552. _w = 16
  1553. _h = 16
  1554. from_x_min = INPUT_START_POS_X
  1555. from_y_min = INPUT_START_POS_Y
  1556. target_x = 40 # min
  1557. target_y = 60 # min
  1558. ang=0
  1559. #read_block_pix()
  1560. read_wall_pix()
  1561. _wall_data = wall_data[:]
  1562. for i,_wall in enumerate(_wall_data):
  1563. #print(_wall)
  1564. if len(_wall) <=6:
  1565. continue
  1566. try:
  1567. if type(_wall[1]) is str:
  1568. continue
  1569. if type(_wall[2]) is str:
  1570. continue
  1571. if type(_wall[3]) is str:
  1572. continue
  1573. if type(_wall[4]) is str:
  1574. continue
  1575. if type(_wall[5]) is str:
  1576. continue
  1577. from_x=from_x_min+_wall[1]#*_w
  1578. from_y=from_y_min+_wall[2]#*_h
  1579. _a= _wall[3]
  1580. ang=0
  1581. if _a < 0:
  1582. ang=0
  1583. elif _a > 0 and _a <= 90:
  1584. ang = 90
  1585. elif _a > 90 and _a <= 180:
  1586. ang = 180
  1587. elif _a > 180 and _a <= 270:
  1588. ang = 270
  1589. x_k = _wall[4]
  1590. y_k = _wall[5]
  1591. target_x=40+(x_k-1)*_w
  1592. target_y=60+(y_k-1)*_h
  1593. sub = grab(from_x,from_y,_w,_h)
  1594. key="{}:{}".format(x_k,y_k)
  1595. W_ANG[key] = ang
  1596. if sub:
  1597. #sub = grab(from_x,from_y,_w,_h)
  1598. sub = pygame.transform.rotate(sub,ang)
  1599. window.blit(sub,(target_x,target_y))
  1600. except Exception as e:
  1601. cprint(" ",_wall,color="red")
  1602. cprint("WALL err:",e,color="red")
  1603. def reload_grid():
  1604. print("==== reload_grid")
  1605. global GRID
  1606. global GRID_A
  1607. try:
  1608. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1609. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1610. except Exception as e:
  1611. cprint("Except: grid re init",e,color="red")
  1612. W_ANG = {}
  1613. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1614. if not NR:
  1615. return
  1616. n = 0
  1617. _max_row = 40
  1618. _y=0
  1619. for i in range(8): # ROW / ZEILEN -> ->
  1620. _x=0
  1621. OUT_COUNT = len(CFG_OUT)
  1622. for j in range(8*OUT_COUNT): # COLUMS / SPALTEN | |
  1623. #pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1624. pygame.draw.rect(window,[10,20,10],[6,6,14,9]) #background
  1625. _j=j
  1626. key="{}:{}".format(_j+1,i+1)
  1627. fr = font12.render(key,1, (200,200,55))
  1628. window.blit(fr,(6,6))
  1629. #window.blit(fr,(40+_x+2,60+_y+2))
  1630. ang = W_ANG.get(key,0)
  1631. sub = grab(6,6,14,9)
  1632. if sub: # rotate nr label
  1633. sub = pygame.transform.rotate(sub,ang)
  1634. if ang == 270: # pos compensation
  1635. window.blit(sub,(40+_x+6,60+_y+1))
  1636. elif ang == 180:# pos compensation
  1637. window.blit(sub,(40+_x+1,60+_y+6))
  1638. else:
  1639. window.blit(sub,(40+_x+1,60+_y+1))
  1640. #if ang != 0:
  1641. # print("W_ANG",key,ang)
  1642. _x+=16 # pix
  1643. _y +=16 # pix
  1644. def draw_counter(COUNTER):
  1645. for count in COUNTER:
  1646. cpan = 0
  1647. ctilt = 0
  1648. cr=255
  1649. cg=255
  1650. cb=255
  1651. csize=10
  1652. cdim=0
  1653. k = "DIM"
  1654. if k in count:
  1655. cdim = int(count[k])
  1656. k = "RED"
  1657. if k in count:
  1658. cr = int(count[k])
  1659. k = "GREEN"
  1660. if k in count:
  1661. cg = int(count[k])
  1662. k = "BLUE"
  1663. if k in count:
  1664. cb = int(count[k])
  1665. k = "SIZE"
  1666. if k in count:
  1667. csize = int(count[k]) #/60*p)
  1668. if csize < 5:
  1669. csize = 5
  1670. k = "PAN"
  1671. if k in count:
  1672. cpan = int(count[k])/255*(block[0] *(_x))
  1673. #cpan = int(cpan * 1.2)
  1674. cpan = int(cpan)
  1675. k = "TILT"
  1676. if k in count:
  1677. ctilt = int(count[k])/255*(block[1] *(_y))
  1678. #ctilt = int(cpan * 1.2)
  1679. ctilt = int(ctilt)
  1680. ddim = cdim/255
  1681. if "DIM" in count and count["DIM"] > 0:
  1682. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1683. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1684. _sec = count["_SEC"]
  1685. try:
  1686. _sec = int(count["_SEC"])
  1687. except:
  1688. pass
  1689. if type(_sec) is int:
  1690. #print(_sec)
  1691. if _sec <= 60:
  1692. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1693. else:
  1694. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1695. #print("_xx",_xx)
  1696. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1697. else:
  1698. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1699. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1700. pygame.draw.rect(window,[0,0,0],fr_r)
  1701. window.blit(fr,fr_r)
  1702. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1703. # overlay
  1704. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1705. font15 = pygame.font.SysFont("freemonobold",17)
  1706. rgb = [255,100,0]
  1707. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1708. _line = lines[0]
  1709. fr = font15.render(_line ,1, (0,0,0))
  1710. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1711. _line = lines[1]
  1712. fr = font15.render(_line ,1, (0,0,0))
  1713. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1714. #_line = lines[2]
  1715. #fr = font15.render(_line ,1, (0,0,0))
  1716. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1717. def draw_all_video_overlay(VIDEO):
  1718. global videplayer
  1719. i=0
  1720. for count in VIDEO:
  1721. video1 = videoplayer[i]
  1722. #video1.overlay_draw(window,"run") # old
  1723. lines = video1._overlay_text()
  1724. video_overlay_draw(wn=window,lines=lines,i=i)
  1725. i += 1
  1726. def del_all_video():
  1727. global videplayer
  1728. i = 0
  1729. print()
  1730. cprint("del_all_video")
  1731. for video in videoplayer:
  1732. cprint("- del video",video)
  1733. video._del()
  1734. def draw_all_video(VIDEO):
  1735. global videplayer
  1736. i = 0
  1737. # set DMX-VALUE to videoplayer Object
  1738. for count in VIDEO:
  1739. cpan = 0
  1740. ctilt = 0
  1741. cr=255
  1742. cg=255
  1743. cb=255
  1744. csize=10
  1745. cang=0
  1746. cdim=0
  1747. video1 = videoplayer[i]
  1748. k = "VIDEO"
  1749. if k in count:
  1750. #video1.select_video(count[k])
  1751. if type(count[k]) is str:
  1752. cprint("ERR count[k] is str not int !")
  1753. continue
  1754. play_nr = int(count[k]/10)
  1755. if play_nr != video1._media_nr:
  1756. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1757. _vid = video1._id
  1758. _cdmx = video1.dmx
  1759. del_video1 = video1
  1760. video1 = PixelMedia(cdmx,_id=_vid)
  1761. video1.select_video(count[k])
  1762. videoplayer[i] = video1
  1763. del_video1._del()
  1764. del del_video1
  1765. k = "DIM"
  1766. if k in count:
  1767. cdim = int(count[k])
  1768. #video1.dim = cdim
  1769. #if i == 0:
  1770. # print(i,cdim)
  1771. k = "ANG"
  1772. if k in count:
  1773. cang = int(count[k])
  1774. k = "SIZE"
  1775. if k in count:
  1776. csize = int(count[k]/16*p)
  1777. if csize < 5:
  1778. csize = 5
  1779. k = "PAN"
  1780. cpan_max = block[0] *(_x) #+block[0]
  1781. if k in count:
  1782. cpan = int(count[k]) / 255*cpan_max
  1783. cpan = int(cpan)
  1784. k = "TILT"
  1785. ctilt_max = block[1] *(_y) #+block[1]
  1786. if k in count:
  1787. ctilt = int(count[k]) / 255*ctilt_max
  1788. ctilt = int(ctilt)
  1789. k = "_reset"
  1790. if k in count:
  1791. if count[k]:
  1792. count[k] = 0
  1793. video1.restart()
  1794. k = "_RUN"
  1795. if k in count:
  1796. video1._run = count[k]
  1797. #video1.pos
  1798. #video1.x=40+0+cpan
  1799. #video1.y=60+0+pm_wy+ctilt
  1800. x=CFG_IN["x1"]+cpan
  1801. y=CFG_IN["y1"]+ctilt
  1802. scale = int((csize))
  1803. angle = int(360-(cang))
  1804. video1.update_dmx(x,y,scale,angle,cdim)
  1805. if cdim:
  1806. video1.next()
  1807. i += 1
  1808. # draw
  1809. i=0
  1810. for count in VIDEO:
  1811. video1 = videoplayer[i]
  1812. video1.draw(window) #,x=0,y=0)
  1813. i+=1
  1814. def counter_dmx(COUNTER,dataA):
  1815. for count in COUNTER:
  1816. cDMX=count["DMX"]-1
  1817. try:
  1818. count["DIM"] = dataA[cDMX]
  1819. count["PAN"] = dataA[cDMX+1]
  1820. count["TILT"] = dataA[cDMX+2]
  1821. count["CONTROL"] = dataA[cDMX+3]
  1822. count["SIZE"] = dataA[cDMX+4]
  1823. count["SEC"] = dataA[cDMX+5]
  1824. count["RED"] = dataA[cDMX+6]
  1825. count["GREEN"] = dataA[cDMX+7]
  1826. count["BLUE"] = dataA[cDMX+8]
  1827. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1828. count["_RUN"] = 0
  1829. count["_pause"] = 0
  1830. count["_time"] = time.time()
  1831. count["_start"] = int(count["SEC"])
  1832. count["_SEC"] = int(count["_start"])
  1833. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1834. if count["_RUN"] == 0:
  1835. print("CONTROL SET TO PAUSE",self)
  1836. count["_RUN"] = 0
  1837. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1838. if count["_RUN"] == 1:
  1839. print("CONTROL SET TO RUN",self)
  1840. count["_RUN"] = 1
  1841. if count["_RUN"]:
  1842. try:
  1843. if count["_pause"] > 0:
  1844. count["_time"] += time.time()-count["_pause"]
  1845. count["_pause"] = 0
  1846. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1847. except Exception as e:
  1848. pass
  1849. else:
  1850. if count["_pause"] == 0:
  1851. count["_pause"] = time.time()
  1852. if type(count["_SEC"]) is int:
  1853. if count["_SEC"] < 0:
  1854. count["_SEC"] = 0
  1855. for ti in range(10):
  1856. #print(ti,(ti+6)*10)
  1857. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1858. count["_SEC"] = "----" #text 1
  1859. try:
  1860. count["_SEC"] = TEXT_BLOCK[ti]
  1861. except Exception as e:
  1862. pass
  1863. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1864. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1865. except Exception as e:
  1866. cprint("EXC FUNC",e,count,color="red")
  1867. #print(count)
  1868. def video_dmx(VIDEO,dataA):
  1869. for count in VIDEO:
  1870. cDMX=count["DMX"]-1
  1871. try:
  1872. count["DIM"] = dataA[cDMX]
  1873. count["PAN"] = dataA[cDMX+1]
  1874. count["TILT"] = dataA[cDMX+2]
  1875. count["CONTROL"] = dataA[cDMX+3]
  1876. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1877. count["_reset"] = 1
  1878. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1879. if count["_RUN"] == 1:
  1880. print( "_RUN:0",count["DMX"]-1)
  1881. count["_RUN"] = 0
  1882. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1883. if count["_RUN"] == 0:
  1884. print( "_RUN:1",count["DMX"]-1)
  1885. count["_RUN"] = 1
  1886. count["SIZE"] = dataA[cDMX+4]#*2
  1887. count["SEC"] = dataA[cDMX+5]
  1888. #count["RED"] = dataA[cDMX+6]
  1889. count["VIDEO"] = dataA[cDMX+6]
  1890. #count["GREEN"] = dataA[cDMX+7]
  1891. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1892. #count["BLUE"] = dataA[cDMX+8]
  1893. except Exception as e:
  1894. cprint("VIDEOPLAYER EXCEPT FUNC",e,count,color="red")
  1895. #print(count)
  1896. def read_dmx_data(ip,ips):
  1897. ip = select_ip(ips,univ=START_UNIV)
  1898. IP = ip
  1899. data = read_dmx(ip)
  1900. ip = select_ip(ips,univ=START_UNIV+1)
  1901. data3 = read_dmx(ip)
  1902. data.extend(data3)
  1903. ip = select_ip(ips,univ=START_UNIV+2)
  1904. data3 = read_dmx(ip)
  1905. data.extend(data3)
  1906. ip = select_ip(ips,univ=START_UNIV+3)
  1907. data3 = read_dmx(ip)
  1908. data.extend(data3)
  1909. if options.dual_vpu:
  1910. ip = select_ip(ips,univ=START_UNIV+4)
  1911. data3 = read_dmx(ip)
  1912. data.extend(data3)
  1913. ip = select_ip(ips,univ=START_UNIV+5)
  1914. data3 = read_dmx(ip)
  1915. data.extend(data3)
  1916. return data
  1917. def set_pos(GRID,_x=0,_y=0):
  1918. for fix in GRID:
  1919. pos = fix.POS(_x,_y)#40,60+pm_wy)
  1920. for subfix in fix.sub_fix:
  1921. #subfix.dmc_calc(data)
  1922. spos = subfix.POS(_x,_y)
  1923. def draw_gobo(GRID,data,_x=0,_y=0):
  1924. i = 0
  1925. dmx = 1
  1926. h = 1
  1927. v = 1
  1928. for fix in GRID:
  1929. rgb = fix.rgb
  1930. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  1931. pygame.draw.rect(window,rgb,pos)
  1932. # DRAW SUB-FIXTURE
  1933. j = 0
  1934. for subfix in fix.sub_fix:#calc(data):
  1935. subfix.dmx_calc(data)
  1936. spos = subfix.POS() #_x,_y)#+pm_wy)
  1937. srgb = subfix.rgb
  1938. if FUNC > 10 and FUNC <= 20: # big dot
  1939. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1940. elif FUNC > 20 and FUNC <= 30:#small dot
  1941. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1942. elif FUNC > 30 and FUNC <= 40:#donut
  1943. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1944. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1945. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1946. pygame.draw.rect(window,srgb,spos)
  1947. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1948. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1949. pygame.draw.rect(window,srgb,spos)
  1950. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1951. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1952. pygame.draw.rect(window,srgb,spos)
  1953. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1954. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1955. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1956. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1957. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1958. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1959. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1960. pygame.draw.rect(window,srgb,spos)
  1961. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1962. elif FUNC > 100 and FUNC <= 110: # rec big line
  1963. pygame.draw.rect(window,srgb,spos)
  1964. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1965. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1966. pygame.draw.rect(window,srgb,spos)
  1967. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1968. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1969. elif FUNC > 120 and FUNC <= 130: # rec inline
  1970. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1971. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1972. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1973. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1974. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1975. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1976. else:
  1977. pygame.draw.rect(window,srgb,spos)
  1978. i += 1
  1979. def grid_label(GRID,_x=0,_y=0):
  1980. for fix in GRID:
  1981. #pos = fix.POS(_x,_y)
  1982. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  1983. rgb = fix.rgb
  1984. # draw row/col grid number
  1985. if pos[0] == 0:
  1986. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  1987. window.blit(fr,(10,pos[1]+3 ))
  1988. if pos[1] == 0:
  1989. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  1990. window.blit(fr,(pos[0]+2,35 ))
  1991. def out_frame():
  1992. rgb = [255,255,0]
  1993. rgb = [255,0,0]
  1994. for CFG_OUTx in CFG_OUT:
  1995. if CFG_OUTx["on"]:
  1996. rgb = [255,0,0]
  1997. p1 = CFG_OUTx["p1"][:]
  1998. p2 = CFG_OUTx["p2"][:]
  1999. p1[0] += 4
  2000. p2[0] -= 4
  2001. draw_frame(window,rgb,p1,p2,offset=3)
  2002. ips=[]
  2003. dataA=[]
  2004. data=[]
  2005. def dmx_raw():
  2006. global frame
  2007. frame += 1
  2008. global ips,dataA,data
  2009. ips = read_index()
  2010. # ----
  2011. ip = select_ip(ips,univ=6) # univ 1 gobo
  2012. dataA = read_dmx(ip)
  2013. # ----
  2014. #data = read_dmx_data(ip,ips)
  2015. if options.countdown:
  2016. #counter_dmx(COUNTER,dataA)
  2017. pass
  2018. if len(VIDEO) > 0:
  2019. video_dmx(VIDEO,dataA)
  2020. return ips,dataA,data
  2021. def dmx_loop():
  2022. while 1:
  2023. dmx_raw()
  2024. time.sleep(1/40) # fast high cpu
  2025. if QUIT:
  2026. break
  2027. thread.start_new_thread(dmx_loop,())
  2028. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2029. dim_raw = value
  2030. pygame.draw.rect(window,[5,5,155],[CFG["x1"]+2,CFG["y2"]+5,CFG["w"]-6,25]) # background
  2031. # GRID_A1_DIM_LABEL
  2032. fr = font15.render("Name: "+name,1, (255,255,255))
  2033. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2034. window.blit(fr,(CFG["x1"]+4,CFG["y2"]+5))
  2035. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2036. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2037. window.blit(fr,(CFG["x1"]+64,CFG["y2"]+20))
  2038. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2039. window.blit(fr,(CFG["x1"]+4,CFG["y2"]+20))
  2040. def _check_dim(_dim):
  2041. dim = 255
  2042. try:
  2043. dim = int(_dim)
  2044. if dim > 255:
  2045. dim = 255
  2046. if dim <=0:
  2047. dim = 0
  2048. except:
  2049. pass
  2050. return dim
  2051. def _GRID_DIM(dim,x,y):
  2052. dim = _check_dim(dim)
  2053. dim_raw = dim*-1
  2054. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2055. s.set_alpha(dim) # alpha level
  2056. s.fill((0,0,0)) # this fills the entire surface
  2057. window.blit(s, (x,y))
  2058. def GRID_DIM():
  2059. for CFG in CFG_OUT:
  2060. dim_ch = int(CFG["DMX-CH"])
  2061. dim = dataA[dim_ch-1]
  2062. _GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2063. draw_output_label(CFG,dim,dim_ch,name=CFG["name"])
  2064. def cvImageToSurface(cvImage):
  2065. if cvImage.dtype.name == 'uint16':
  2066. cvImage = (cvImage / 256).astype('uint8')
  2067. size = cvImage.shape[1::-1]
  2068. if len(cvImage.shape) == 2:
  2069. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2070. format = 'RGB'
  2071. else:
  2072. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2073. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2074. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2075. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2076. def create_img_meta():
  2077. out = {}
  2078. #40 235
  2079. out["pos"] = (0,0) # x,y
  2080. out["ipos"] = (0,0) # x,y
  2081. out["scale"] = 50
  2082. out["dim"] = 100
  2083. out["ang"] = 0
  2084. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2085. out["fname"] = "picker.png"
  2086. out["raw_img"] = ""
  2087. out["img"] = ""
  2088. out["err"] = []
  2089. return out
  2090. def img_open(img_meta):
  2091. cprint("img_open",img_meta["fname"],"-------")
  2092. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2093. im2 = cv2.imread(fname)
  2094. width = int(im2.shape[1])
  2095. height = int(im2.shape[0])
  2096. w_max = 480
  2097. h_max = 320
  2098. #w_max = 600
  2099. #h_max = 400
  2100. w_max = 4800 # max input jpg resolution
  2101. h_max = 3200 # max input jpg resolution
  2102. if width > w_max:
  2103. h = int(height*(w_max/width))
  2104. w = w_max
  2105. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2106. if height > h_max:
  2107. w = int(width*(h_max/height))
  2108. h = h_max
  2109. if w < w_max:
  2110. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2111. img_meta["raw_img"] = im2
  2112. img_meta["img"] = im2
  2113. return im2
  2114. def img_jitter(img_meta):
  2115. r1=random.randint(0,3)
  2116. r2=random.randint(0,3)
  2117. img_meta["ipos"] = [r1,r2]
  2118. def img_rescale_width(img_meta, _width):
  2119. frame = img_meta["raw_img"]
  2120. width = int(frame.shape[1])
  2121. height = int(frame.shape[0])
  2122. dim = (width, height)
  2123. if 1: #width > _width:
  2124. h = int(height*( width/_width))
  2125. dim = (_width, h)
  2126. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2127. else:
  2128. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2129. return im
  2130. def img_rescale(img, percent=75):
  2131. frame = img
  2132. #frame = img_meta["raw_img"]
  2133. width = int(frame.shape[1] * percent/ 100)
  2134. height = int(frame.shape[0] * percent/ 100)
  2135. dim = (width, height)
  2136. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2137. def img_draw_raw(img_meta):
  2138. #img_jitter(img_meta)
  2139. pos = img_meta["pos"]
  2140. ipos = img_meta["ipos"]
  2141. im2 = img_meta["img"]
  2142. dim = img_meta["dim"]
  2143. im2 = cvImageToSurface(im2)
  2144. w2 = int(im2.get_width()/2)
  2145. h2 = int(im2.get_height()/2)
  2146. x=pos[0]+ipos[0]
  2147. y=pos[1]+ipos[1]
  2148. window.blit(im2, (x-w2,y-h2))
  2149. img_meta_a = create_img_meta()
  2150. count_tilt = 0
  2151. def main():
  2152. global IP
  2153. global GRID
  2154. global GRID_A
  2155. global FUNC
  2156. global count_tilt
  2157. global TEXT_BLOCK
  2158. global TEXT_BLOCK_TIME
  2159. global MEDIALIST
  2160. global MEDIALIST_TIME
  2161. global dataA
  2162. global frame2
  2163. global runnung
  2164. reload_grid()
  2165. s=time.time()
  2166. r = ""
  2167. IP = "xx"
  2168. while running:
  2169. if TEXT_BLOCK_TIME+5 < time.time():
  2170. TEXT_BLOCK = open_text_block()
  2171. TEXT_BLOCK_TIME = time.time()
  2172. if MEDIALIST_TIME+6 < time.time():
  2173. MEDIALIST = open_medialist()
  2174. MEDIALIST_TIME = time.time()
  2175. pygame.display.flip()
  2176. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2177. event()
  2178. window.fill((10,0,30))
  2179. calc_fps()
  2180. calc_fps2()
  2181. # GRID loop
  2182. try:
  2183. FUNC = dataA[gobo_ch-1]
  2184. FUNC2 = FUNC
  2185. except Exception as e:
  2186. print("EXC FUNC",e)
  2187. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2188. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2189. if VIDEO:
  2190. draw_all_video(VIDEO)
  2191. if options.countdown:
  2192. draw_counter(COUNTER)
  2193. # output background mask
  2194. _w = 8*16
  2195. if len(out_idim):
  2196. _w = len(out_idim)*8*16
  2197. pygame.draw.rect(window,[0,0,0],[35,57,_w+16,135]) # background
  2198. pointer.draw(0,pm_wy) #wy
  2199. spos = [0,0,0,0]
  2200. if PIXEL_MAPPING >= 1:
  2201. #try:
  2202. if options.dual_vpu:
  2203. GRID_X = GRID_A[8*8:]
  2204. xx = p*8
  2205. reshape2(GRID,GRID_A,CFG_OUT[0]["x1"],CFG_OUT[0]["y1"],name="GRID_A1") #start pos
  2206. if options.out_idim:
  2207. out_frame()
  2208. # OUTPUT -> MAPING
  2209. if options.out_idim:
  2210. draw_kachel_nr()
  2211. if options.out_idim:
  2212. input_mask() # mask background
  2213. if options.out_idim:
  2214. output_mask() # mask background
  2215. GRID_DIM() # overlay
  2216. if VIDEO:
  2217. draw_all_video_overlay(VIDEO)
  2218. draw_main_overlay()
  2219. draw_marker()
  2220. pointer.draw(0,pm_wy) #wy
  2221. pygame.display.flip()
  2222. clock.tick(25)
  2223. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2224. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2225. frame2 += 1
  2226. if __name__ == "__main__":
  2227. try:
  2228. main()
  2229. finally:
  2230. del_all_video()