vpu_live4.py 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750
  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. for cdmx in dmx_start:
  689. if len(videoplayer) > max_videoplayer:
  690. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  691. break
  692. print("-- videoplayer dmx:",cdmx)
  693. try:
  694. cdmx = int(cdmx)
  695. videoplayer.append( PixelMedia(cdmx,_id=_vid) )
  696. _vid += 1
  697. _tmp = {"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127
  698. ,"CONTROL":0,"SEC":10,"VIDEO":"3"
  699. ,"RED":255,"GREEN":255,"BLUE":255
  700. ,"_time":time.time()
  701. ,"_RUN":0,"_SEC":">{}<".format(cdmx)
  702. }
  703. VIDEO.append(_tmp)
  704. except Exception as e:
  705. cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
  706. def loop_videoplayer():
  707. while 1:
  708. _videoplayer = videoplayer[:]
  709. #print(".")
  710. ok = 0
  711. for i in _videoplayer: #.append( PixelMedia(cdmx,_id=_vid) )
  712. try:
  713. r = i.read() # read next frame from file
  714. if r:
  715. ok = 1
  716. except Exception as e:
  717. cprint("EXCEPTION loop_videoplayer ",e,color="red")
  718. cprint(traceback.format_exc(),color="red")
  719. cprint(sys.exc_info()[2],color="red")
  720. cprint()
  721. if ok == 0:
  722. time.sleep(0.1)
  723. else:
  724. time.sleep(0.005)
  725. if QUIT:
  726. break
  727. thread.start_new_thread(loop_videoplayer,())
  728. p = 16
  729. block = [p,p]
  730. _x = 8
  731. _y = 8
  732. #HD = "0"
  733. if options.mode:
  734. try:
  735. HD = options.mode
  736. p,_x,_y = HD.split(",")
  737. _x = int(_x)
  738. _y = int(_y)
  739. p = int(p)
  740. block = [p,p]
  741. except Exception as e:
  742. cprint( "Exc",options.mode,e,color="red")
  743. # PARSE COMMANDLINE ARGUMENTS
  744. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":0,"y2":0 ,"w":0,"h":300}
  745. # OUTPUT CFG INIT
  746. CFG_OUT = []
  747. out_idim = options.out_idim
  748. if out_idim:
  749. out_idim = options.out_idim.split(",")
  750. x1 = 40
  751. for i,v in enumerate(out_idim):
  752. print("--out-idim",[i,v])
  753. _t={}
  754. try:
  755. v = int(v)
  756. _t = {"name":"CFG_OUT{}".format(i+1),"x1":x1,"y1":60,"x2":0,"y2":0 ,"w":30,"h":30,"on":1,"DMX-CH":v}
  757. print(_t,v)
  758. CFG_OUT.append(_t)
  759. x1 += p*8
  760. except Exception as e:
  761. print("err",_t,color="red")
  762. print("err",e,color="red")
  763. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  764. if _x < 1:
  765. _x = 1
  766. if _y < 1:
  767. _y = 1
  768. CFG_BLOCK["h-count"] = _x
  769. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  770. CFG_BLOCK["size"] = p
  771. print( [options.xsplit])
  772. print( [options.ysplit])
  773. try:
  774. if options.xsplit:
  775. CFG_BLOCK["h-split"] = int(options.xsplit)
  776. if options.ysplit:
  777. CFG_BLOCK["v-split"] = int(options.ysplit)
  778. except Exception as e:
  779. cprint( "Exc",options.mode,e,color="red")
  780. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  781. print("xy",_x,_y)
  782. print("++++++++++++++++++", p,_x,_y)
  783. _x2 = _x
  784. try:
  785. if options.XX:
  786. pass#_x2 = int(options.XX)
  787. except Exception as e:
  788. cprint( "Exc",options.mode,e,color="red")
  789. print("_x2 , -X",_x2)
  790. # ===== GUI =========
  791. import pygame
  792. import pygame.gfxdraw
  793. import pygame.font
  794. clock = pygame.time.Clock()
  795. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  796. if options.win_pos:
  797. if "," in options.win_pos:
  798. win_pos = options.win_pos.split(",")
  799. try:
  800. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  801. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  802. except Excetpion as e:
  803. cprint("win_pos",win_pos,e,color="red")
  804. os.environ['SDL_VIDEO_CENTERED'] = '0'
  805. pg = pygame
  806. pygame.init()
  807. pygame.mixer.quit()
  808. f = pygame.font.get_fonts()
  809. for i in f:
  810. if "mono" in i.lower():
  811. print(i)
  812. font = pygame.font.SysFont("freemonobold",22)
  813. font10 = pygame.font.SysFont("freemonobold",10)
  814. font12 = pygame.font.SysFont("freemonobold",12)
  815. font12l = pygame.font.SysFont("freemono",12)
  816. font15 = pygame.font.SysFont("freemonobold",15)
  817. font40 = pygame.font.SysFont("freemonobold",40)
  818. font80 = pygame.font.SysFont("freemonobold",70)
  819. #font = pygame.font.SysFont(None,30)
  820. fr = font.render("hallo" ,1, (200,0,255))
  821. def clean_path(path):
  822. _path = path[:]
  823. _path = _path.replace("/","-")
  824. _path = _path.replace(".","-")
  825. _path = _path.replace("\"","-")
  826. _path = _path.replace("'","-")
  827. return _path
  828. PIXEL_MAPPING = 0
  829. vpu_wall_conf_file = HOME+"/LibreLight/vpu_wall_conf.csv"
  830. grid_file = "/tmp/vpu_grid_hd.csv"
  831. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  832. media_list = HOME+"/LibreLight/video/" #.format(path)
  833. pm_wy = 0
  834. if options.pixel_mapping:
  835. PIXEL_MAPPING = 1
  836. path = options.pixel_mapping
  837. path = clean_path(path)
  838. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  839. if options.dual_vpu:
  840. grid_file = HOME+"/LibreLight/vpu_grid_dual{}.csv".format(path)
  841. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  842. media_list = HOME+"/LibreLight/video/" #.format(path)
  843. if not options.out_idim:
  844. PIXEL_MAPPING = 0
  845. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  846. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  847. MAIN_SIZE=(600,500)
  848. try:
  849. if _x < 8 and PIXEL_MAPPING >= 1:
  850. wx = 30+30+block[0] * 8
  851. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  852. else:
  853. wx = 30+30+block[0] * _x
  854. wy = 40+40+block[1] * _y
  855. if type(options.videoplayer) is str:
  856. wy += 150 # video medialist
  857. MAIN_SIZE=(wx,wy)
  858. if PIXEL_MAPPING >= 1:
  859. pm_wy = 11*p #+ p*3
  860. CFG_IN["y1"] += 11*p
  861. if options.dual_vpu:
  862. if wx < p*16+155:
  863. wx = p*16+155
  864. MAIN_SIZE=(wx,wy+pm_wy)
  865. except Exception as e:
  866. cprint("Exception:",e,color="red")
  867. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  868. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  869. for CFG_OUTx in CFG_OUT:
  870. CFG_OUTx["w"] = CFG_BLOCK["size"] * 8
  871. CFG_OUTx["h"] = CFG_BLOCK["size"] * 8
  872. def CFG_CALC_P(CFG):
  873. CFG["x2"] = CFG["x1"]+CFG["w"]
  874. CFG["y2"] = CFG["y1"]+CFG["h"]
  875. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  876. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  877. print("CFG",CFG)
  878. # ?
  879. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  880. CFG_CALC_P(CFG_IN)
  881. for CFG_OUTx in CFG_OUT:
  882. CFG_CALC_P(CFG_OUTx)
  883. print("CFG_BLOCK",CFG_BLOCK)
  884. print()
  885. print()
  886. print()
  887. print()
  888. print()
  889. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  890. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  891. class Fix():
  892. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  893. #print("Fix",_id)
  894. self._id = _id
  895. self.dmx = (_id-1) * ch +1 #dmx
  896. self.univ = univ
  897. self.ch = ch
  898. self.pos = pos
  899. self.rgb = [0,0,0]
  900. self.block = block #[10,10]
  901. self.x = pos[0]
  902. self.y = pos[1]
  903. self._x = 0
  904. self._y = 0
  905. self.strobo = time.time()
  906. self.bmp = 250
  907. self.sub_fix = []
  908. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  909. if _id <= 0: #exit
  910. return
  911. spalte = (_id-1)%_y +1
  912. zeile = int((_id-1)/_x2) #+1
  913. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  914. #zeile 1
  915. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  916. sid = sid+1
  917. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  918. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  919. self.sub_fix.append(sub_fix)
  920. sid = sid+1
  921. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  922. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  923. self.sub_fix.append(sub_fix)
  924. #zeile 2
  925. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  926. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  927. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  928. self.sub_fix.append(sub_fix)
  929. #sid = sid+1
  930. sid = sid+1
  931. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  932. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  933. self.sub_fix.append(sub_fix)
  934. def __repr__(self):
  935. return "<Fix: {} {} {} {}>".format(id(self),self.x,self.y,self.dmx)
  936. def calc(self,data):
  937. _rgb = [0,255,0]
  938. return _rgb
  939. def sub_calc(self,data):
  940. _rgb = [0,255,0]
  941. for sub_fix in self.sub_fix:
  942. sub_fix.block = self.block[:]
  943. _rgb = sub_fix.dmx_calc(data)
  944. return _rgb
  945. def _calc(self,a,b):
  946. A = (self.pos[0])*self.block[0]
  947. B = (self.pos[1])*self.block[1]
  948. C = self.block[0]-a
  949. D = self.block[1]-b
  950. return A,B,C,D
  951. def POS(self,x=None,y=None,a=0,b=0):
  952. if type(x) is int:
  953. self._x=x
  954. if type(y) is int:
  955. self._y=y
  956. A,B,C,D = self._calc(a,b)
  957. return [self._x+A,self._y+B,C,D]
  958. def XXXsubPOS(self,x=0,y=0,a=0,b=0):
  959. __out = []
  960. for sub_fix in self.sub_fix:
  961. __out.append( sub_fix.POS(x,y,a,b) )
  962. return __out
  963. class SubFix():
  964. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  965. #print("Fix",_id)
  966. self._id = _id
  967. self.dmx = (_id-1) * ch +1 #dmx
  968. self.univ = univ
  969. self.ch = ch
  970. self.pos = pos
  971. self.rgb = [0,0,40]
  972. self.block = block #[10,10]
  973. self.x = pos[0]
  974. self.y = pos[1]
  975. self._x = 0
  976. self._y = 0
  977. self.strobo = time.time()
  978. self.bmp = 250
  979. def dmx_calc(self,data):
  980. #return [130,30,20]
  981. dmx_sub = [30]*10
  982. dmx = self.dmx -1
  983. _dmx_sub = []
  984. if self.dmx >= 0:
  985. dmx = rDMX(self.univ,self.dmx)-1
  986. if dmx+self.ch < len(data):
  987. _dmx_sub = data[dmx:dmx+self.ch]
  988. if _dmx_sub:
  989. dmx_sub = _dmx_sub
  990. dim = dmx_sub[0]/255
  991. r = dmx_sub[1]*dim
  992. g = dmx_sub[2]*dim
  993. b = dmx_sub[3]*dim
  994. r = int(r)
  995. g = int(g)
  996. b = int(b)
  997. self.rgb = [r,g,b]
  998. return self.rgb
  999. def _calc(self,a,b):
  1000. A = (self.pos[0]) #+self.block[0]
  1001. B = (self.pos[1]) #+self.block[1]
  1002. C = self.block[0]-a
  1003. D = self.block[1]-b
  1004. return A,B,C,D
  1005. def POS(self,x=None,y=None,a=0,b=0):
  1006. if type(x) is int:
  1007. self._x=x
  1008. if type(y) is int:
  1009. self._y=y
  1010. A,B,C,D = self._calc(a,b)
  1011. #if NR:
  1012. # C-=1
  1013. # D-=1
  1014. return [int(self._x+A),int(self._y+B),int(C),int(D)]
  1015. class POINTER():
  1016. def __init__(self):
  1017. self.pos = [0,0,0,0]
  1018. self.on = 0
  1019. self.rgb = [0,100,10]
  1020. self._x = 0
  1021. self._y = 0
  1022. self.x = 0
  1023. self.y = 0
  1024. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  1025. def row_move(self,x,y):
  1026. self._x = x
  1027. self._y = y
  1028. def move(self,pos):
  1029. self.pos = pos
  1030. self.on = 1
  1031. def cross(self,x,y):
  1032. self.x = x
  1033. self.y = y
  1034. def draw(self,x,y):
  1035. pos = self.pos[:]
  1036. #print("draw",x,y,pos)
  1037. pos[0] += x
  1038. pos[1] += y
  1039. fix_x= self.fix.x
  1040. fix_y= self.fix.y +y
  1041. if 0:# self.on:
  1042. pygame.draw.rect(window,self.rgb,pos)
  1043. # mouse grid posision
  1044. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  1045. _nr = fix_y * _x + fix_x +1
  1046. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1047. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1048. window.blit(fr,(130,1))
  1049. if 1:# lupe / looking-glass / lens
  1050. __t = 60
  1051. f_x = CFG_IN["y2"]+5
  1052. rgb=[255,0,0]
  1053. pos=[400,400,160,160]
  1054. pos=[400,f_x,160,160]
  1055. #pos=[400,400,f_x,f_x]
  1056. draw_box_frame(window,rgb,pos,offset=1)
  1057. r_sub = grab(x=self._x-__t/2,y=self._y-__t/2,w=__t,h=__t)
  1058. if r_sub:
  1059. r_sub = pygame.transform.scale(r_sub,(160,160))
  1060. if r_sub: # target
  1061. window.blit(r_sub, (400,f_x))
  1062. #pos=[400+40,400+40,80,80]
  1063. pos=[400+80,400+80,80,80]
  1064. pos=[400+80,f_x+80,80,80]
  1065. draw_box_frame(window,rgb,pos,offset=3)
  1066. draw_box_frame(window,rgb,pos,offset=2)
  1067. draw_box_frame(window,rgb,pos,offset=0)
  1068. draw_box_frame(window,rgb,pos,offset=1)
  1069. # fix pos
  1070. #txt=str(pos) #"[0, 0, 0, 0]"
  1071. #fr = font15.render(txt ,1, (200,200,200))
  1072. #window.blit(fr,(10,1))
  1073. # pointer
  1074. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1075. window.blit(fr,(10,30))
  1076. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1077. window.blit(fr,(10,40))
  1078. if 1: #self._x > 30 and self._y > 230:
  1079. # pointer
  1080. _x = self._x-INPUT_START_POS_X
  1081. _y = self._y-INPUT_START_POS_Y
  1082. rgb = [200,200,200]
  1083. if _x < 0:
  1084. rgb = [200,0,0]
  1085. fr = font40.render("X:{:03}".format(_x) ,1, rgb)
  1086. f_x = CFG_IN["y2"]+8
  1087. window.blit(fr,(100,f_x))
  1088. rgb = [200,200,200]
  1089. if _y < 0:
  1090. rgb = [200,0,0]
  1091. fr = font40.render("Y:{:03}".format(_y) ,1, rgb)
  1092. window.blit(fr,(200,f_x))
  1093. p=110
  1094. # crosshair
  1095. self.rgb = [0,0,200]
  1096. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),2 )
  1097. #pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),2 )
  1098. self.rgb = [0,100,200]
  1099. pygame.draw.line(window,self.rgb, (self.x,self.y-p/3) , (self.x,self.y-2),2 )
  1100. self.rgb = [0,200,0]
  1101. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),2 )
  1102. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),2 )
  1103. self.rgb = [200,0,0]
  1104. pointer = POINTER()
  1105. NR = 0
  1106. running = True
  1107. def event():
  1108. global NR,running
  1109. for event in pygame.event.get():
  1110. #print(event.dict)
  1111. _button = None
  1112. if "button" in event.dict:
  1113. _button = event.dict["button"]
  1114. _state = None
  1115. if "state" in event.dict:
  1116. _state = event.state
  1117. _key = None
  1118. if "key" in event.dict:
  1119. _key = event.key
  1120. _pos = None
  1121. if "pos" in event.dict:
  1122. _pos = event.pos
  1123. _type = None
  1124. if "type" in event.dict:
  1125. _type = event.type
  1126. _type = event.type
  1127. _mod = None
  1128. if "mod" in event.dict:
  1129. _mod = event.mod
  1130. if 0:
  1131. print( " ")
  1132. print( "{:.02f}".format( time.time() - START ))
  1133. print("button -",_button,end="\t| ")
  1134. #print("state -",_state)
  1135. print("pos -",_pos)
  1136. print("type -",_type, end="\t| ")
  1137. print("key -",_key)
  1138. print("mod -",_mod)
  1139. try:
  1140. if _type in [5,1025]:
  1141. if _button == 1:
  1142. NR += 1
  1143. if NR > 1:
  1144. NR = 0
  1145. if _button == 3:
  1146. NR -= 1
  1147. if NR < 0:
  1148. NR = 1
  1149. if _pos:
  1150. posA = _pos
  1151. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1152. if fix:
  1153. #pos = fix.POS(40,60+pm_wy)
  1154. pos = fix.POS() #(CFG_IN["x1"],CFG_IN["y1"])#0,0) #+pm_wy)
  1155. rgb = [0,0,0]
  1156. pointer.move(pos)
  1157. pointer.fix = fix
  1158. else:
  1159. pointer.on = 0
  1160. pointer.row_move(_pos[0],_pos[1])
  1161. pointer.cross(_pos[0],_pos[1])
  1162. if event.type == pygame.VIDEORESIZE:
  1163. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1164. except Exception as e:
  1165. cprint(e,color="red")
  1166. if event.type==pygame.QUIT:
  1167. global QUIT
  1168. QUIT = 1
  1169. cprint("QUIT",color="red")
  1170. running=False
  1171. #time.sleep(1)
  1172. #sys.exit()
  1173. fps = 0
  1174. fps2 = 0
  1175. frame = 0
  1176. frame2 = 0
  1177. frame_t = time.time()
  1178. frame2_t = time.time()
  1179. IP = "yyy"
  1180. vplay=[]
  1181. vplay1=0
  1182. vplay2=0
  1183. if options.videoplayer:
  1184. try:
  1185. t=options.videoplayer.split(",")
  1186. vplay=t
  1187. vplay1=t[0]
  1188. vplay2=t[1]
  1189. except:pass
  1190. def output_mask(): # oben
  1191. RGB = [0,0,50]
  1192. #RGB = [120,0,0] # test
  1193. start = 0
  1194. # oben
  1195. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,57]) # background
  1196. # unten
  1197. pygame.draw.rect(window,RGB,[0,start+192,MAIN_SIZE[0]+200,35]) # background
  1198. # links
  1199. pygame.draw.rect(window,RGB,[0,start+0,35,235]) # background
  1200. def input_mask(): # unten
  1201. RGB = [0,0,50]
  1202. #RGB = [120,0,110] # test
  1203. start = 227
  1204. #f_x = CFG_IN["x2"]-50
  1205. f_x = CFG_IN["y2"]+3
  1206. # oben
  1207. pygame.draw.rect(window,RGB,[0,start,MAIN_SIZE[0]+200,7]) # background
  1208. # links
  1209. pygame.draw.rect(window,RGB,[0,start,35,200]) # background
  1210. # unten
  1211. #pygame.draw.rect(window,RGB,[0,start+173,MAIN_SIZE[0]+200,198]) # background
  1212. pygame.draw.rect(window,RGB,[0,f_x,MAIN_SIZE[0]+200,198]) # background
  1213. def draw_main_overlay():
  1214. global fps,fps2
  1215. fr = font15.render("DMX-FPS: {}".format(fps) ,1, (200,0,255))
  1216. window.blit(fr,(10,2))
  1217. fr = font15.render("GUI-FPS : {}".format(fps2) ,1, (200,0,255))
  1218. window.blit(fr,(10,12))
  1219. #fr = font15.render("ip:{}".format(IP) ,1, (200,0,255))
  1220. #window.blit(fr,(100,2))
  1221. fr = font15.render("start-uni: {:}.xx dRGB".format(START_UNIV) ,1, (200,0,255))
  1222. window.blit(fr,(90,2))
  1223. fr = font15.render("gobo_ch1: {}.{:}".format(START_UNIV_A,options.gobo_ch) ,1, (200,0,255))
  1224. window.blit(fr,(210,2))
  1225. fr = font15.render("gobo_ch2: {}.{:}".format(START_UNIV_A,options.gobo_ch2) ,1, (200,0,255))
  1226. window.blit(fr,(210,12))
  1227. for i,v in enumerate(vplay):
  1228. fr = font15.render("v-play{}: {}.{:}".format(i+1,START_UNIV_A,v) ,1, (200,0,255))
  1229. window.blit(fr,(300,2+12*i))
  1230. try:
  1231. global pointer
  1232. # pointer
  1233. fr = font15.render("X:{:03}".format(pointer._x) ,1, (200,200,200))
  1234. window.blit(fr,(10,30))
  1235. fr = font15.render("Y:{:03}".format(pointer._y) ,1, (200,200,200))
  1236. window.blit(fr,(10,40))
  1237. except Exception as e:
  1238. print("Overlay err:",e)
  1239. def draw_marker():
  1240. # kennlinie linker rand
  1241. # limit marker INPUT AREA
  1242. rgb = [0,127,255] # orange
  1243. for CFG_OUTx in CFG_OUT: #["y1"]
  1244. f_x = CFG_OUTx["y1"]
  1245. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1246. f_x = CFG_OUTx["y2"]
  1247. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1248. if CFG_OUT:
  1249. f_x = CFG_OUT[0]["y1"]
  1250. for i,v in enumerate("OUTPUT"):
  1251. fr = font15.render(v ,1, rgb) #(200,0,255))
  1252. if v == "I":
  1253. window.blit(fr,(10+2,f_x+i*15+15))
  1254. else:
  1255. window.blit(fr,(10,f_x+i*15+15))
  1256. # kennlinie linker rand
  1257. # limit marker INPUT AREA
  1258. rgb = [255,127,0] # orange
  1259. f_x = CFG_IN["y1"]
  1260. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1261. f_x = CFG_IN["y2"]
  1262. pygame.draw.line(window,rgb,[0,f_x],[20,f_x])
  1263. f_x = CFG_IN["y1"]
  1264. for i,v in enumerate("INPUT"):
  1265. fr = font15.render(v ,1, rgb) #(200,0,255))
  1266. if v == "I":
  1267. window.blit(fr,(10+2,f_x+i*15+15))
  1268. else:
  1269. window.blit(fr,(10,f_x+i*15+15))
  1270. def draw_box_frame(window,rgb,pos,offset=0):
  1271. p1 = [pos[0],pos[1]]
  1272. p2 = [pos[0]+pos[2],pos[1]+pos[3]]
  1273. o = offset
  1274. _p1 = [p1[0]-o,p1[1]-o]
  1275. _p2 = [p1[0]-o,p2[1]+o]
  1276. pygame.draw.line(window,rgb,_p1,_p2) # left
  1277. _p1 = [p2[0]+o,p2[1]+o]
  1278. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1279. _p2 = [p2[0]+o,p1[1]-o]
  1280. pygame.draw.line(window,rgb,_p1,_p2) # right
  1281. _p1 = [p1[0]-o,p1[1]-o]
  1282. pygame.draw.line(window,rgb,_p1,_p2) # top
  1283. def draw_frame(window,rgb,p1,p2,offset=0):
  1284. o = offset
  1285. _p1 = [p1[0]-o,p1[1]-o]
  1286. _p2 = [p1[0]-o,p2[1]+o]
  1287. #pygame.draw.line(window,rgb,_p1,_p2) # left
  1288. _p1 = [p2[0]+o,p2[1]+o]
  1289. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1290. _p2 = [p2[0]+o,p1[1]-o]
  1291. #pygame.draw.line(window,rgb,_p1,_p2) # right
  1292. _p1 = [p1[0]-o,p1[1]-o]
  1293. pygame.draw.line(window,rgb,_p1,_p2) # top
  1294. def calc_fps():
  1295. global fps,frame,frame_t
  1296. t = time.time()
  1297. if frame_t+1 < t:
  1298. fps = frame #frame_t- t #frame
  1299. frame = 1
  1300. frame_t = time.time()
  1301. def calc_fps2():
  1302. global fps2,frame2,frame2_t
  1303. t = time.time()
  1304. if frame2_t+0.1 < t:
  1305. fps2 = frame2*10 #frame_t- t #frame
  1306. frame2 = 1
  1307. frame2_t = t #time.time()
  1308. TEXT_BLOCK = []
  1309. def _create_text_block():
  1310. print("======== CREATE NEW TEXT FILE !!",text_file)
  1311. f = open(text_file,"w")
  1312. for i in range(10):
  1313. f.write("TEXT {}\n".format(i+1))
  1314. f.close()
  1315. def open_text_block():
  1316. print("======== OPEN TEXT FILE !!",text_file)
  1317. _lines = []
  1318. try:
  1319. f = open(text_file,"r")
  1320. _lines = f.readlines()
  1321. f.close()
  1322. except FileNotFoundError as e:
  1323. print("TEXT",e)
  1324. _create_text_block()
  1325. if len(_lines) <= 0:
  1326. _create_text_block()
  1327. lines = []
  1328. for l in _lines:
  1329. #print(">> ",l.strip())
  1330. lines.append(l.strip())
  1331. if len(lines) <= 10:
  1332. for i in range(10-len(lines)):
  1333. lines.append("LINE ERROR")
  1334. return lines
  1335. TEXT_BLOCK = open_text_block()
  1336. TEXT_BLOCK_TIME = time.time()
  1337. # video medialist
  1338. MEDIALIST = []
  1339. def open_medialist():
  1340. print("======== OPEN MEDIALIST DIR !!",media_list)
  1341. if not os.path.isdir(media_list):
  1342. os.system("mkdir -p {}".format(media_list))
  1343. _lines = os.listdir(media_list)
  1344. _lines.sort()
  1345. lines = ['']*25
  1346. i=0
  1347. for l in _lines:
  1348. l = l.strip()
  1349. if "_" in l:
  1350. ll = l.split("_",1)
  1351. #print(">> ",ll)
  1352. try:
  1353. lll = int(ll[0])
  1354. lines[lll] = l
  1355. except:pass
  1356. if len(lines) <= 10:
  1357. for i in range(10-len(lines)):
  1358. lines.append("")#"LINE ERROR")
  1359. return lines
  1360. MEDIALIST_TIME = time.time()
  1361. MEDIALIST = open_medialist()
  1362. # ===== GUI =========
  1363. def draw_circle(surface,color, pos, radius):
  1364. x,y=pos
  1365. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1366. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1367. def rDMX(univ,dmx):
  1368. return univ*512+dmx
  1369. def generate_grid(mapping=0):
  1370. _log = []
  1371. #if PIXEL_MAPPING:
  1372. # log = open(grid_file,"w")
  1373. head = "i,univ,dmx,x,y,ch\n"
  1374. head = "i,univ,dmx,ch\n"
  1375. head = "univ,dmx,x,y,ch\n"
  1376. head = "nr,id,info\n"
  1377. print("csv:",head)
  1378. _log.append(head)
  1379. dmx = 1-1
  1380. ch = 4
  1381. y=0
  1382. x=0
  1383. for i in range((_y)*(_x)):
  1384. #if x > _x and i%_x == 0:
  1385. if x > 8 and i%8 == 0:
  1386. #print("--> -->")
  1387. x=0
  1388. y+=1
  1389. _univ = int(dmx/512)
  1390. _dmx = dmx - (_univ)*512
  1391. pos=[x,y]
  1392. line="{},{},x\n".format(i+1,i+1)
  1393. _log.append(line)
  1394. dmx += ch
  1395. x+=1
  1396. if mapping and PIXEL_MAPPING:
  1397. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1398. log = open(grid_file,"w")
  1399. log.writelines(_log)
  1400. log.close()
  1401. return _log[:] #GRID
  1402. def init_grid(mapping=0,_x=4,_y=4,_xp=0,_yp=0,start=0,name="init_gird"):
  1403. if mapping and PIXEL_MAPPING:
  1404. try:
  1405. log = open(grid_file,"r")
  1406. except:
  1407. generate_grid(mapping=mapping)
  1408. log = open(grid_file,"r")
  1409. lines = log.readlines()
  1410. else:
  1411. lines = generate_grid()
  1412. GRID = []
  1413. y=0
  1414. x=0
  1415. print("CSV header",[lines[0]],[PIXEL_MAPPING,"len:",len(lines),"start:",start,name])
  1416. for i,line in enumerate(lines[1+start:]): #exclude first line
  1417. #print("rcsv",[line])
  1418. line = line.strip()
  1419. line = line.split(",") # csv
  1420. if i >= _x and i%_x == 0:
  1421. x=0
  1422. y+=1
  1423. if y >= _y:
  1424. break
  1425. _id = int(line[1])
  1426. pos = [x+_xp,y+_yp]
  1427. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1428. GRID.append(f)
  1429. x+=1
  1430. return GRID
  1431. def find_pix(x,y):
  1432. global GRID
  1433. for fix in GRID:
  1434. X = 0
  1435. Y = 0
  1436. pos = fix.POS()
  1437. if x > pos[0] and x < pos[0]+pos[2]:
  1438. X = 1
  1439. if y > pos[1] and y < pos[1]+pos[3]:
  1440. Y = 1
  1441. if X and Y:
  1442. return fix
  1443. GRID = []
  1444. GRID_A = []
  1445. NR = 0
  1446. START_UNIV_A=1
  1447. START_UNIV=2
  1448. if options.start_univ:
  1449. try:
  1450. START_UNIV=int(options.start_univ)
  1451. except Exception as e:
  1452. print("Exception START UNIV",e)
  1453. gobo_ch=1
  1454. if options.gobo_ch:
  1455. try:
  1456. gobo_ch=int(options.gobo_ch)
  1457. except Exception as e:
  1458. print("Exception gobo_ch",e)
  1459. if gobo_ch <= 0:
  1460. gobo_ch = 1
  1461. def grab(x=55,y=55,w=60,h=60):
  1462. crop = None
  1463. rect = pygame.Rect(x, y, w, h)
  1464. try:
  1465. sub = window.subsurface(rect)
  1466. crop = pygame.Surface((w,h))
  1467. crop.blit(sub, (0,0))
  1468. except ValueError as e:
  1469. #cprint("grab():",e,x,y,w,h,color="red")
  1470. pass
  1471. return crop
  1472. vpu_wall_txt="""---,quelle,,---,ziel,---,---
  1473. out,x,y,ang,row,col,info
  1474. Wall1,0,0,0,1,1,
  1475. Wall1,0,0,0,2,2,
  1476. Wall1,0,0,0,3,3,
  1477. Wall1,0,0,0,4,4,
  1478. Wall1,0,0,0,5,5,
  1479. Wall1,0,0,0,6,6,
  1480. Wall1,0,0,0,7,7,
  1481. ,,,,,,
  1482. Wall2,0,0,0,4,1,----
  1483. Wall2,0,0,0,5,2,
  1484. Wall2,4,4,0,6,3,
  1485. Wall2,0,0,0,7,4,
  1486. Wall2,0,0,0,8,5,
  1487. Wall2,0,0,0,9,6,
  1488. ,,,,,,
  1489. ,,,,,,
  1490. Wall3,64,48,0,12,1,----
  1491. Wall3,64,64,0,12,2,
  1492. Wall3,64,80,0,12,3,
  1493. Wall3,64,96,0,12,4,
  1494. Wall3,64,112,0,12,5,
  1495. Wall3,64,128,0,12,6,"""
  1496. def create_vpu_wall_conf():
  1497. f=open(vpu_wall_conf_file,"w")
  1498. f.write(vpu_wall_txt)
  1499. f.close()
  1500. print("CREATE VPU CONFIG !")
  1501. wall_data = []
  1502. _last_wall_config_time = time.time()
  1503. def read_wall_pix(init=0):
  1504. global _last_wall_config_time
  1505. global wall_data, NR
  1506. grid = []
  1507. #if not NR and init==0:
  1508. # return
  1509. ok = 0
  1510. if init:
  1511. ok = 1
  1512. if NR:
  1513. if time.time()-_last_wall_config_time > 3:
  1514. ok = 1
  1515. else:
  1516. if time.time()-_last_wall_config_time > 10:
  1517. ok = 1
  1518. if not ok:
  1519. return
  1520. print("read_wall_pix",vpu_wall_conf_file)
  1521. _last_wall_config_time = time.time()
  1522. if not os.path.isfile(vpu_wall_conf_file):
  1523. create_vpu_wall_conf()
  1524. f=open(vpu_wall_conf_file,"r")
  1525. lines = f.readlines()
  1526. for i,line in enumerate(lines):
  1527. line = line.strip()
  1528. if not line:
  1529. continue
  1530. line = line.split(",")
  1531. if not line[0]:
  1532. continue
  1533. #print(i,line)
  1534. if len(line) >= 3:
  1535. _l = []
  1536. for i in line:
  1537. try:
  1538. _l.append(int(i))
  1539. except Exception as e:
  1540. _l.append(i)
  1541. grid.append(_l)
  1542. #for i,v in enumerate(grid):
  1543. # print(i,v)
  1544. wall_data = grid
  1545. read_wall_pix(init=1)
  1546. def reshape2(GRID,GRID_OUT,_x,_y,name="GRID_Z"):
  1547. global W_ANG
  1548. _w = 16
  1549. _h = 16
  1550. from_x_min = INPUT_START_POS_X
  1551. from_y_min = INPUT_START_POS_Y
  1552. target_x = 40 # min
  1553. target_y = 60 # min
  1554. ang=0
  1555. #read_block_pix()
  1556. read_wall_pix()
  1557. _wall_data = wall_data[:]
  1558. for i,_wall in enumerate(_wall_data):
  1559. #print(_wall)
  1560. if len(_wall) <=6:
  1561. continue
  1562. try:
  1563. if type(_wall[1]) is str:
  1564. continue
  1565. if type(_wall[2]) is str:
  1566. continue
  1567. if type(_wall[3]) is str:
  1568. continue
  1569. if type(_wall[4]) is str:
  1570. continue
  1571. if type(_wall[5]) is str:
  1572. continue
  1573. from_x=from_x_min+_wall[1]#*_w
  1574. from_y=from_y_min+_wall[2]#*_h
  1575. _a= _wall[3]
  1576. ang=0
  1577. if _a < 0:
  1578. ang=0
  1579. elif _a > 0 and _a <= 90:
  1580. ang = 90
  1581. elif _a > 90 and _a <= 180:
  1582. ang = 180
  1583. elif _a > 180 and _a <= 270:
  1584. ang = 270
  1585. x_k = _wall[4]
  1586. y_k = _wall[5]
  1587. target_x=40+(x_k-1)*_w
  1588. target_y=60+(y_k-1)*_h
  1589. sub = grab(from_x,from_y,_w,_h)
  1590. key="{}:{}".format(x_k,y_k)
  1591. W_ANG[key] = ang
  1592. if sub:
  1593. #sub = grab(from_x,from_y,_w,_h)
  1594. sub = pygame.transform.rotate(sub,ang)
  1595. window.blit(sub,(target_x,target_y))
  1596. except Exception as e:
  1597. cprint(" ",_wall,color="red")
  1598. cprint("WALL err:",e,color="red")
  1599. def reload_grid():
  1600. print("==== reload_grid")
  1601. global GRID
  1602. global GRID_A
  1603. try:
  1604. GRID = init_grid(_x=_x,_y=_y,name="GRID") #init_gird()
  1605. GRID_A = init_grid(_x=8,_y=16,mapping=1,name="GRID_A") #init_gird()
  1606. except Exception as e:
  1607. cprint("Except: grid re init",e,color="red")
  1608. W_ANG = {}
  1609. def draw_kachel_nr(): #GRID,_x=0,_y=0):
  1610. if not NR:
  1611. return
  1612. n = 0
  1613. _max_row = 40
  1614. _y=0
  1615. for i in range(8): # ROW / ZEILEN -> ->
  1616. _x=0
  1617. OUT_COUNT = len(CFG_OUT)
  1618. for j in range(8*OUT_COUNT): # COLUMS / SPALTEN | |
  1619. #pygame.draw.rect(window,[10,20,10],[40+_x+2,60+_y+2,14,9]) #background
  1620. pygame.draw.rect(window,[10,20,10],[6,6,14,9]) #background
  1621. _j=j
  1622. key="{}:{}".format(_j+1,i+1)
  1623. fr = font12.render(key,1, (200,200,55))
  1624. window.blit(fr,(6,6))
  1625. #window.blit(fr,(40+_x+2,60+_y+2))
  1626. ang = W_ANG.get(key,0)
  1627. sub = grab(6,6,14,9)
  1628. if sub: # rotate nr label
  1629. sub = pygame.transform.rotate(sub,ang)
  1630. if ang == 270: # pos compensation
  1631. window.blit(sub,(40+_x+6,60+_y+1))
  1632. elif ang == 180:# pos compensation
  1633. window.blit(sub,(40+_x+1,60+_y+6))
  1634. else:
  1635. window.blit(sub,(40+_x+1,60+_y+1))
  1636. #if ang != 0:
  1637. # print("W_ANG",key,ang)
  1638. _x+=16 # pix
  1639. _y +=16 # pix
  1640. def draw_counter(COUNTER):
  1641. for count in COUNTER:
  1642. cpan = 0
  1643. ctilt = 0
  1644. cr=255
  1645. cg=255
  1646. cb=255
  1647. csize=10
  1648. cdim=0
  1649. k = "DIM"
  1650. if k in count:
  1651. cdim = int(count[k])
  1652. k = "RED"
  1653. if k in count:
  1654. cr = int(count[k])
  1655. k = "GREEN"
  1656. if k in count:
  1657. cg = int(count[k])
  1658. k = "BLUE"
  1659. if k in count:
  1660. cb = int(count[k])
  1661. k = "SIZE"
  1662. if k in count:
  1663. csize = int(count[k]) #/60*p)
  1664. if csize < 5:
  1665. csize = 5
  1666. k = "PAN"
  1667. if k in count:
  1668. cpan = int(count[k])/255*(block[0] *(_x))
  1669. #cpan = int(cpan * 1.2)
  1670. cpan = int(cpan)
  1671. k = "TILT"
  1672. if k in count:
  1673. ctilt = int(count[k])/255*(block[1] *(_y))
  1674. #ctilt = int(cpan * 1.2)
  1675. ctilt = int(ctilt)
  1676. ddim = cdim/255
  1677. if "DIM" in count and count["DIM"] > 0:
  1678. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1679. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1680. _sec = count["_SEC"]
  1681. try:
  1682. _sec = int(count["_SEC"])
  1683. except:
  1684. pass
  1685. if type(_sec) is int:
  1686. #print(_sec)
  1687. if _sec <= 60:
  1688. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1689. else:
  1690. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1691. #print("_xx",_xx)
  1692. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1693. else:
  1694. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1695. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1696. pygame.draw.rect(window,[0,0,0],fr_r)
  1697. window.blit(fr,fr_r)
  1698. def video_overlay_draw(wn=None,lines=["line1","line2"],i=0):
  1699. # overlay
  1700. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(i+1)*35,380,28])
  1701. font15 = pygame.font.SysFont("freemonobold",17)
  1702. rgb = [255,100,0]
  1703. pygame.draw.rect(wn,rgb,[5,MAIN_SIZE[1]-(i+1)*35,200,13])
  1704. _line = lines[0]
  1705. fr = font15.render(_line ,1, (0,0,0))
  1706. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35))
  1707. _line = lines[1]
  1708. fr = font15.render(_line ,1, (0,0,0))
  1709. wn.blit(fr,(10,MAIN_SIZE[1]-(i+1)*35+15))
  1710. #_line = lines[2]
  1711. #fr = font15.render(_line ,1, (0,0,0))
  1712. #wn.blit(fr,(75,MAIN_SIZE[1]-(i+1)*35+15))
  1713. def draw_all_video_overlay(VIDEO):
  1714. global videplayer
  1715. i=0
  1716. for count in VIDEO:
  1717. video1 = videoplayer[i]
  1718. #video1.overlay_draw(window,"run") # old
  1719. lines = video1._overlay_text()
  1720. video_overlay_draw(wn=window,lines=lines,i=i)
  1721. i += 1
  1722. def del_all_video():
  1723. global videplayer
  1724. i = 0
  1725. print()
  1726. cprint("del_all_video")
  1727. for video in videoplayer:
  1728. cprint("- del video",video)
  1729. video._del()
  1730. def draw_all_video(VIDEO):
  1731. global videplayer
  1732. i = 0
  1733. # set DMX-VALUE to videoplayer Object
  1734. for count in VIDEO:
  1735. cpan = 0
  1736. ctilt = 0
  1737. cr=255
  1738. cg=255
  1739. cb=255
  1740. csize=10
  1741. cang=0
  1742. cdim=0
  1743. video1 = videoplayer[i]
  1744. k = "VIDEO"
  1745. if k in count:
  1746. #video1.select_video(count[k])
  1747. if type(count[k]) is str:
  1748. cprint("ERR count[k] is str not int !")
  1749. continue
  1750. play_nr = int(count[k]/10)
  1751. if play_nr != video1._media_nr:
  1752. print( "+ + + + + + + + + + +", play_nr , video1._media_nr ,str(video1))
  1753. _vid = video1._id
  1754. _cdmx = video1.dmx
  1755. del_video1 = video1
  1756. video1 = PixelMedia(cdmx,_id=_vid)
  1757. video1.select_video(count[k])
  1758. videoplayer[i] = video1
  1759. del_video1._del()
  1760. del del_video1
  1761. k = "DIM"
  1762. if k in count:
  1763. cdim = int(count[k])
  1764. #video1.dim = cdim
  1765. #if i == 0:
  1766. # print(i,cdim)
  1767. k = "ANG"
  1768. if k in count:
  1769. cang = int(count[k])
  1770. k = "SIZE"
  1771. if k in count:
  1772. csize = int(count[k]/16*p)
  1773. if csize < 5:
  1774. csize = 5
  1775. k = "PAN"
  1776. cpan_max = block[0] *(_x) #+block[0]
  1777. if k in count:
  1778. cpan = int(count[k]) / 255*cpan_max
  1779. cpan = int(cpan)
  1780. k = "TILT"
  1781. ctilt_max = block[1] *(_y) #+block[1]
  1782. if k in count:
  1783. ctilt = int(count[k]) / 255*ctilt_max
  1784. ctilt = int(ctilt)
  1785. k = "_reset"
  1786. if k in count:
  1787. if count[k]:
  1788. count[k] = 0
  1789. video1.restart()
  1790. k = "_RUN"
  1791. if k in count:
  1792. video1._run = count[k]
  1793. #video1.pos
  1794. #video1.x=40+0+cpan
  1795. #video1.y=60+0+pm_wy+ctilt
  1796. x=CFG_IN["x1"]+cpan
  1797. y=CFG_IN["y1"]+ctilt
  1798. scale = int((csize))
  1799. angle = int(360-(cang))
  1800. video1.update_dmx(x,y,scale,angle,cdim)
  1801. if cdim:
  1802. video1.next()
  1803. i += 1
  1804. # draw
  1805. i=0
  1806. for count in VIDEO:
  1807. video1 = videoplayer[i]
  1808. video1.draw(window) #,x=0,y=0)
  1809. i+=1
  1810. def counter_dmx(COUNTER,dataA):
  1811. for count in COUNTER:
  1812. cDMX=count["DMX"]-1
  1813. try:
  1814. count["DIM"] = dataA[cDMX]
  1815. count["PAN"] = dataA[cDMX+1]
  1816. count["TILT"] = dataA[cDMX+2]
  1817. count["CONTROL"] = dataA[cDMX+3]
  1818. count["SIZE"] = dataA[cDMX+4]
  1819. count["SEC"] = dataA[cDMX+5]
  1820. count["RED"] = dataA[cDMX+6]
  1821. count["GREEN"] = dataA[cDMX+7]
  1822. count["BLUE"] = dataA[cDMX+8]
  1823. if count["CONTROL"] >= 10 and count["CONTROL"] < 20: # RESET COUNTER
  1824. count["_RUN"] = 0
  1825. count["_pause"] = 0
  1826. count["_time"] = time.time()
  1827. count["_start"] = int(count["SEC"])
  1828. count["_SEC"] = int(count["_start"])
  1829. if count["CONTROL"] >= 20 and count["CONTROL"] < 30: # PAUSE COUNTER
  1830. count["_RUN"] = 0
  1831. if count["CONTROL"] >= 30 and count["CONTROL"] < 40: # PLAY COUNTER
  1832. count["_RUN"] = 1
  1833. if count["_RUN"]:
  1834. try:
  1835. if count["_pause"] > 0:
  1836. count["_time"] += time.time()-count["_pause"]
  1837. count["_pause"] = 0
  1838. count["_SEC"] = int(count["_start"] - (time.time() - count["_time"]))
  1839. except Exception as e:
  1840. pass
  1841. else:
  1842. if count["_pause"] == 0:
  1843. count["_pause"] = time.time()
  1844. if type(count["_SEC"]) is int:
  1845. if count["_SEC"] < 0:
  1846. count["_SEC"] = 0
  1847. for ti in range(10):
  1848. #print(ti,(ti+6)*10)
  1849. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1850. count["_SEC"] = "----" #text 1
  1851. try:
  1852. count["_SEC"] = TEXT_BLOCK[ti]
  1853. except Exception as e:
  1854. pass
  1855. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1856. count["_SEC"] = "DMX:{}-{}".format(cDMX+1,cDMX+8+1)
  1857. except Exception as e:
  1858. cprint("EXC FUNC",e,count,color="red")
  1859. #print(count)
  1860. def video_dmx(VIDEO,dataA):
  1861. for count in VIDEO:
  1862. cDMX=count["DMX"]-1
  1863. try:
  1864. count["DIM"] = dataA[cDMX]
  1865. count["PAN"] = dataA[cDMX+1]
  1866. count["TILT"] = dataA[cDMX+2]
  1867. count["CONTROL"] = dataA[cDMX+3]
  1868. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1869. count["_reset"] = 1
  1870. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1871. if count["_RUN"] == 1:
  1872. print( "_RUN:0",count["DMX"]-1)
  1873. count["_RUN"] = 0
  1874. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1875. if count["_RUN"] == 0:
  1876. print( "_RUN:1",count["DMX"]-1)
  1877. count["_RUN"] = 1
  1878. count["SIZE"] = dataA[cDMX+4]#*2
  1879. count["SEC"] = dataA[cDMX+5]
  1880. #count["RED"] = dataA[cDMX+6]
  1881. count["VIDEO"] = dataA[cDMX+6]
  1882. #count["GREEN"] = dataA[cDMX+7]
  1883. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1884. #count["BLUE"] = dataA[cDMX+8]
  1885. except Exception as e:
  1886. cprint("VIDEOPLAYER EXCEPT FUNC",e,count,color="red")
  1887. #print(count)
  1888. def read_dmx_data(ip,ips):
  1889. ip = select_ip(ips,univ=START_UNIV)
  1890. IP = ip
  1891. data = read_dmx(ip)
  1892. ip = select_ip(ips,univ=START_UNIV+1)
  1893. data3 = read_dmx(ip)
  1894. data.extend(data3)
  1895. ip = select_ip(ips,univ=START_UNIV+2)
  1896. data3 = read_dmx(ip)
  1897. data.extend(data3)
  1898. ip = select_ip(ips,univ=START_UNIV+3)
  1899. data3 = read_dmx(ip)
  1900. data.extend(data3)
  1901. if options.dual_vpu:
  1902. ip = select_ip(ips,univ=START_UNIV+4)
  1903. data3 = read_dmx(ip)
  1904. data.extend(data3)
  1905. ip = select_ip(ips,univ=START_UNIV+5)
  1906. data3 = read_dmx(ip)
  1907. data.extend(data3)
  1908. return data
  1909. def set_pos(GRID,_x=0,_y=0):
  1910. for fix in GRID:
  1911. pos = fix.POS(_x,_y)#40,60+pm_wy)
  1912. for subfix in fix.sub_fix:
  1913. #subfix.dmc_calc(data)
  1914. spos = subfix.POS(_x,_y)
  1915. def draw_gobo(GRID,data,_x=0,_y=0):
  1916. i = 0
  1917. dmx = 1
  1918. h = 1
  1919. v = 1
  1920. for fix in GRID:
  1921. rgb = fix.rgb
  1922. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  1923. pygame.draw.rect(window,rgb,pos)
  1924. # DRAW SUB-FIXTURE
  1925. j = 0
  1926. for subfix in fix.sub_fix:#calc(data):
  1927. subfix.dmx_calc(data)
  1928. spos = subfix.POS() #_x,_y)#+pm_wy)
  1929. srgb = subfix.rgb
  1930. if FUNC > 10 and FUNC <= 20: # big dot
  1931. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1932. elif FUNC > 20 and FUNC <= 30:#small dot
  1933. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1934. elif FUNC > 30 and FUNC <= 40:#donut
  1935. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1936. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1937. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1938. pygame.draw.rect(window,srgb,spos)
  1939. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1940. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1941. pygame.draw.rect(window,srgb,spos)
  1942. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1943. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1944. pygame.draw.rect(window,srgb,spos)
  1945. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1946. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1947. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1948. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1949. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1950. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1951. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1952. pygame.draw.rect(window,srgb,spos)
  1953. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1954. elif FUNC > 100 and FUNC <= 110: # rec big line
  1955. pygame.draw.rect(window,srgb,spos)
  1956. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1957. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1958. pygame.draw.rect(window,srgb,spos)
  1959. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1960. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1961. elif FUNC > 120 and FUNC <= 130: # rec inline
  1962. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1963. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1964. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1965. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1966. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1967. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1968. else:
  1969. pygame.draw.rect(window,srgb,spos)
  1970. i += 1
  1971. def grid_label(GRID,_x=0,_y=0):
  1972. for fix in GRID:
  1973. #pos = fix.POS(_x,_y)
  1974. pos = fix.POS() #CFG_IN["x1"],CFG_IN["y1"])
  1975. rgb = fix.rgb
  1976. # draw row/col grid number
  1977. if pos[0] == 0:
  1978. fr = font12.render("{}".format(pos[1]+1) ,1, (200,200,200))
  1979. window.blit(fr,(10,pos[1]+3 ))
  1980. if pos[1] == 0:
  1981. fr = font12.render("{}".format(pos[0]+1) ,1, (200,200,200))
  1982. window.blit(fr,(pos[0]+2,35 ))
  1983. def out_frame():
  1984. rgb = [255,255,0]
  1985. rgb = [255,0,0]
  1986. for CFG_OUTx in CFG_OUT:
  1987. if CFG_OUTx["on"]:
  1988. rgb = [255,0,0]
  1989. p1 = CFG_OUTx["p1"][:]
  1990. p2 = CFG_OUTx["p2"][:]
  1991. p1[0] += 4
  1992. p2[0] -= 4
  1993. draw_frame(window,rgb,p1,p2,offset=3)
  1994. ips=[]
  1995. dataA=[]
  1996. data=[]
  1997. def dmx_raw():
  1998. global frame
  1999. frame += 1
  2000. global ips,dataA,data
  2001. ips = read_index()
  2002. # ----
  2003. ip = select_ip(ips,univ=1) # univ 1 gobo
  2004. dataA = read_dmx(ip)
  2005. # ----
  2006. data = read_dmx_data(ip,ips)
  2007. if options.countdown:
  2008. counter_dmx(COUNTER,dataA)
  2009. if len(VIDEO) > 0:
  2010. video_dmx(VIDEO,dataA)
  2011. return ips,dataA,data
  2012. def dmx_loop():
  2013. while 1:
  2014. dmx_raw()
  2015. time.sleep(1/40) # fast high cpu
  2016. if QUIT:
  2017. break
  2018. thread.start_new_thread(dmx_loop,())
  2019. def draw_output_label(CFG,value,dmx="0.0",name="GRID_XX"):
  2020. dim_raw = value
  2021. pygame.draw.rect(window,[5,5,155],[CFG["x1"]+2,CFG["y2"]+5,CFG["w"]-6,25]) # background
  2022. # GRID_A1_DIM_LABEL
  2023. fr = font15.render("Name: "+name,1, (255,255,255))
  2024. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2025. window.blit(fr,(CFG["x1"]+4,CFG["y2"]+5))
  2026. fr = font15.render("inv-dim: "+str(dim_raw) ,1, (255,255,255))
  2027. fr_r = fr.get_rect(center=(int(wx/2),int(0+pm_wy-p*0-10)))
  2028. window.blit(fr,(CFG["x1"]+64,CFG["y2"]+20))
  2029. fr = font15.render("dmx: {}.{}".format(START_UNIV_A,dmx) ,1, (255,255,255))
  2030. window.blit(fr,(CFG["x1"]+4,CFG["y2"]+20))
  2031. def _check_dim(_dim):
  2032. dim = 255
  2033. try:
  2034. dim = int(_dim)
  2035. if dim > 255:
  2036. dim = 255
  2037. if dim <=0:
  2038. dim = 0
  2039. except:
  2040. pass
  2041. return dim
  2042. def _GRID_DIM(dim,x,y):
  2043. dim = _check_dim(dim)
  2044. dim_raw = dim*-1
  2045. s = pygame.Surface((p*8,p*8)) # the size of your rect
  2046. s.set_alpha(dim) # alpha level
  2047. s.fill((0,0,0)) # this fills the entire surface
  2048. window.blit(s, (x,y))
  2049. def GRID_DIM():
  2050. for CFG in CFG_OUT:
  2051. dim_ch = int(CFG["DMX-CH"])
  2052. dim = dataA[dim_ch-1]
  2053. _GRID_DIM(dim,x=CFG["x1"],y=CFG["y2"]-p*8)
  2054. draw_output_label(CFG,dim,dim_ch,name=CFG["name"])
  2055. def cvImageToSurface(cvImage):
  2056. if cvImage.dtype.name == 'uint16':
  2057. cvImage = (cvImage / 256).astype('uint8')
  2058. size = cvImage.shape[1::-1]
  2059. if len(cvImage.shape) == 2:
  2060. cvImage = np.repeat(cvImage.reshape(size[1], size[0], 1), 3, axis = 2)
  2061. format = 'RGB'
  2062. else:
  2063. format = 'RGBA' if cvImage.shape[2] == 4 else 'RGB'
  2064. cvImage[:, :, [0, 2]] = cvImage[:, :, [2, 0]]
  2065. surface = pygame.image.frombuffer(cvImage.flatten(), size, format)
  2066. return surface.convert_alpha() if format == 'RGBA' else surface.convert()
  2067. def create_img_meta():
  2068. out = {}
  2069. #40 235
  2070. out["pos"] = (0,0) # x,y
  2071. out["ipos"] = (0,0) # x,y
  2072. out["scale"] = 50
  2073. out["dim"] = 100
  2074. out["ang"] = 0
  2075. out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
  2076. out["fname"] = "picker.png"
  2077. out["raw_img"] = ""
  2078. out["img"] = ""
  2079. out["err"] = []
  2080. return out
  2081. def img_open(img_meta):
  2082. cprint("img_open",img_meta["fname"],"-------")
  2083. fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
  2084. im2 = cv2.imread(fname)
  2085. width = int(im2.shape[1])
  2086. height = int(im2.shape[0])
  2087. w_max = 480
  2088. h_max = 320
  2089. #w_max = 600
  2090. #h_max = 400
  2091. w_max = 4800 # max input jpg resolution
  2092. h_max = 3200 # max input jpg resolution
  2093. if width > w_max:
  2094. h = int(height*(w_max/width))
  2095. w = w_max
  2096. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2097. if height > h_max:
  2098. w = int(width*(h_max/height))
  2099. h = h_max
  2100. if w < w_max:
  2101. im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
  2102. img_meta["raw_img"] = im2
  2103. img_meta["img"] = im2
  2104. return im2
  2105. def img_jitter(img_meta):
  2106. r1=random.randint(0,3)
  2107. r2=random.randint(0,3)
  2108. img_meta["ipos"] = [r1,r2]
  2109. def img_rescale_width(img_meta, _width):
  2110. frame = img_meta["raw_img"]
  2111. width = int(frame.shape[1])
  2112. height = int(frame.shape[0])
  2113. dim = (width, height)
  2114. if 1: #width > _width:
  2115. h = int(height*( width/_width))
  2116. dim = (_width, h)
  2117. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2118. else:
  2119. im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2120. return im
  2121. def img_rescale(img, percent=75):
  2122. frame = img
  2123. #frame = img_meta["raw_img"]
  2124. width = int(frame.shape[1] * percent/ 100)
  2125. height = int(frame.shape[0] * percent/ 100)
  2126. dim = (width, height)
  2127. return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
  2128. def img_draw_raw(img_meta):
  2129. #img_jitter(img_meta)
  2130. pos = img_meta["pos"]
  2131. ipos = img_meta["ipos"]
  2132. im2 = img_meta["img"]
  2133. dim = img_meta["dim"]
  2134. im2 = cvImageToSurface(im2)
  2135. w2 = int(im2.get_width()/2)
  2136. h2 = int(im2.get_height()/2)
  2137. x=pos[0]+ipos[0]
  2138. y=pos[1]+ipos[1]
  2139. window.blit(im2, (x-w2,y-h2))
  2140. img_meta_a = create_img_meta()
  2141. count_tilt = 0
  2142. def main():
  2143. global IP
  2144. global GRID
  2145. global GRID_A
  2146. global FUNC
  2147. global count_tilt
  2148. global TEXT_BLOCK
  2149. global TEXT_BLOCK_TIME
  2150. global MEDIALIST
  2151. global MEDIALIST_TIME
  2152. global dataA
  2153. global frame2
  2154. global runnung
  2155. reload_grid()
  2156. s=time.time()
  2157. r = ""
  2158. IP = "xx"
  2159. while running:
  2160. if TEXT_BLOCK_TIME+5 < time.time():
  2161. TEXT_BLOCK = open_text_block()
  2162. TEXT_BLOCK_TIME = time.time()
  2163. if MEDIALIST_TIME+6 < time.time():
  2164. MEDIALIST = open_medialist()
  2165. MEDIALIST_TIME = time.time()
  2166. pygame.display.flip()
  2167. set_pos(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2168. event()
  2169. window.fill((10,0,30))
  2170. calc_fps()
  2171. calc_fps2()
  2172. # GRID loop
  2173. try:
  2174. FUNC = dataA[gobo_ch-1]
  2175. FUNC2 = FUNC
  2176. except Exception as e:
  2177. print("EXC FUNC",e)
  2178. draw_gobo(GRID,data) #,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2179. grid_label(GRID,_x=CFG_IN["x1"],_y=CFG_IN["y1"])
  2180. if VIDEO:
  2181. draw_all_video(VIDEO)
  2182. if options.countdown:
  2183. draw_counter(COUNTER)
  2184. # output background mask
  2185. _w = 8*16
  2186. if len(out_idim):
  2187. _w = len(out_idim)*8*16
  2188. pygame.draw.rect(window,[0,0,0],[35,57,_w+16,135]) # background
  2189. pointer.draw(0,pm_wy) #wy
  2190. spos = [0,0,0,0]
  2191. if PIXEL_MAPPING >= 1:
  2192. #try:
  2193. if options.dual_vpu:
  2194. GRID_X = GRID_A[8*8:]
  2195. xx = p*8
  2196. reshape2(GRID,GRID_A,CFG_OUT[0]["x1"],CFG_OUT[0]["y1"],name="GRID_A1") #start pos
  2197. if options.out_idim:
  2198. out_frame()
  2199. # OUTPUT -> MAPING
  2200. if options.out_idim:
  2201. draw_kachel_nr()
  2202. if options.out_idim:
  2203. input_mask() # mask background
  2204. if options.out_idim:
  2205. output_mask() # mask background
  2206. GRID_DIM() # overlay
  2207. if VIDEO:
  2208. draw_all_video_overlay(VIDEO)
  2209. draw_main_overlay()
  2210. draw_marker()
  2211. pointer.draw(0,pm_wy) #wy
  2212. pygame.display.flip()
  2213. clock.tick(25)
  2214. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  2215. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  2216. frame2 += 1
  2217. if __name__ == "__main__":
  2218. try:
  2219. main()
  2220. finally:
  2221. del_all_video()