vpu_live.py 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376
  1. import math
  2. import random
  3. import time
  4. import os
  5. import sys
  6. import traceback
  7. import gc
  8. HOME = os.getenv('HOME')
  9. import _thread as thread
  10. from optparse import OptionParser
  11. ...
  12. parser = OptionParser()
  13. parser.add_option("-m", "--mode", dest="mode",
  14. help="pixel mode pix,x,y --mode 40,10,8") #, metavar="FILE")
  15. parser.add_option("-X", "--XX", dest="XX", #default=1,
  16. help="x-split") #, metavar="FILE")
  17. parser.add_option("-x", "--xx", dest="xsplit", #default=1,
  18. help="x-split") #, metavar="FILE")
  19. parser.add_option("-y", "--yy", dest="ysplit",#default=1,
  20. help="y-split") #, metavar="FILE")
  21. parser.add_option("", "--start-univ", dest="start_univ",#default=1,
  22. help="set start-univers default=2") #, metavar="FILE")
  23. parser.add_option("", "--gobo-ch", dest="gobo_ch",#default=1,
  24. help="gobo ch univ on 1") #, metavar="FILE")
  25. #os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  26. parser.add_option("", "--win-pos", dest="win_pos",default="200,164",
  27. help="SDL_VIDEO_WINDOW_POS --win-pos=200,164") #, metavar="FILE")
  28. parser.add_option("", "--pixel-mapping", dest="pixel_mapping",default=0,
  29. help="pixel_mapping file/on --pixel-mapping=_x") #, metavar="FILE")
  30. parser.add_option("", "--countdown", dest="countdown",#default=1,
  31. help="enable countdown") #, metavar="FILE")
  32. parser.add_option("", "--videoplayer", dest="videoplayer",#default=1,
  33. help="enable videoplayer") #, metavar="FILE")
  34. parser.add_option("", "--title", dest="title",default="SCREEN",
  35. help="set title") #, metavar="FILE")
  36. #parser.add_option("-f", "--file", dest="filename",
  37. # help="write report to FILE", metavar="FILE")
  38. #parser.add_option("-q", "--quiet",
  39. # action="store_false", dest="verbose", default=True,
  40. # help="don't print status messages to stdout")
  41. (options, args) = parser.parse_args()
  42. import numpy
  43. for o in dir(options):
  44. if "_" in o:
  45. continue
  46. print(o,options.__getattribute__(o))
  47. START = time.time()
  48. # ===== ARTNET DMX =========
  49. import memcache
  50. mc = memcache.Client(['127.0.0.1:11211'], debug=0)
  51. def read_index():
  52. ips=mc.get("index")#cmd)
  53. if ips is None:
  54. ips = {}
  55. #for k,v in ips.items():
  56. # print(k,v)
  57. return ips
  58. def select_ip(ips, univ=2): # artnet univ
  59. _univ = ":{}".format(univ)
  60. for ip in ips: #high priority
  61. if "2.0.0" in ip and _univ in ip:
  62. return ip
  63. for ip in ips:
  64. if "ltp-out" in ip and _univ in ip:
  65. return ip
  66. FUNC = 0
  67. COUNTER = []
  68. if options.countdown:
  69. cdmx_start = options.countdown.split(",")
  70. for cdmx in cdmx_start:
  71. try:
  72. cdmx = int(cdmx)
  73. COUNTER.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  74. except Exception as e:
  75. print("EXCEPTION COUNTER INIT ",cdmx)
  76. def read_dmx(ip):
  77. r = ""
  78. if ip:
  79. #t = int(math.sin(time.time() - s)*10)
  80. r = mc.get(ip) #"2.0.0.13:2")
  81. rr = [0]*512
  82. for i,v in enumerate(r):
  83. try: #cleanup ltp-out to int
  84. v = int(v)
  85. rr[i] = v
  86. except:pass
  87. r = rr
  88. if not r:
  89. c = 0
  90. #time.sleep(0.1)
  91. r = [0] *512
  92. for i in range(12*8+1):
  93. dmx = i*4
  94. #print(dmx)
  95. r[dmx:dmx+4] = [255,10,10,40]
  96. return r
  97. # ===== ARTNET DMX =========
  98. PLAYLIST = []
  99. try:
  100. PLAYLIST = open_playlist()
  101. except:pass
  102. #import json
  103. #import pickle
  104. def scan_capture(name="MiraBox",serial=""):
  105. ls = os.listdir("/dev/")
  106. name = name.upper()
  107. serial = serial.upper()
  108. out = []
  109. for l in ls:
  110. if l.startswith("video"):
  111. #print(l)
  112. cmd="udevadm info --query=all /dev/{}".format(l)
  113. print("# cmd:",cmd)
  114. r = os.popen(cmd)
  115. ok_name = 0
  116. ok_capture = 0
  117. ok_serial = 0
  118. for line in r.readlines():
  119. line = line.strip()
  120. line = line.upper()
  121. #print(l,line)
  122. #ID_V4L_CAPABILITIES=:capture:
  123. if "ID_V4L_CAPABILITIES=:capture:".upper() in line:
  124. ok_capture = 1
  125. if name != "" and "_MODEL" in line and name in line:
  126. ok_name = 1
  127. if serial != "" and "ID_SERIAL_SHORT" in line and serial in line:
  128. ok_serial = 1
  129. if (name == "" or ok_name) and (serial == "" or ok_serial) and ok_capture:
  130. print(l,"# name:",ok_name,"capture:",ok_capture,"serial:",ok_serial,"# OK !",[name,serial])
  131. out.append([l,name,serial])
  132. else:
  133. print("#",l,"# name:",ok_name,"capture:",ok_capture,"serial:",ok_serial,"# FAIL !",[name,serial])
  134. print()
  135. return out
  136. class Vopen():
  137. def __init__(self,dmx=None,_id=None):
  138. global PLAYLIST
  139. self._id = _id
  140. self.fpath = HOME+'/Downloads/'
  141. self.fpath = HOME+'/LibreLight/video/'
  142. self.fname = '' #'bbb_sunflower_480x320.mp4'
  143. #self.fname = 'no-video.mp4'
  144. try:
  145. self.fname = PLAYLIST[0]
  146. except Exception as e:
  147. print("Exception set video from PLAYLIST 5543:",e)
  148. self.restart_t = time.time()
  149. self.fps = 0
  150. self.scale = 50 #%
  151. self.angle = 0 #%
  152. self.dmx=dmx
  153. if self._id >= 250:
  154. self.fname = "cam_"
  155. self.dim = 0
  156. self.x = 0
  157. self.y = 0
  158. self.init_count = 0
  159. self.cap = None
  160. self.shape = [200,200]
  161. self.img = None
  162. self.success = 1
  163. self.cv2 = None
  164. self._run = 0
  165. self.end = 0
  166. self._video_nr = 0
  167. self.shape_x = 370
  168. self.shape_y = 235
  169. try:
  170. global cv2
  171. self.cv2 = cv2
  172. except:
  173. pass
  174. self.init()
  175. def __repr__(self):
  176. sizeof = int(sys.getsizeof(self.buffer)/8)
  177. return "< id:{} buf:{} run:{} fps:{} scale:{} name:{} {}kb>".format(
  178. id(self),len(self.buffer),self._run,self.fps,self.scale
  179. ,self.fname,sizeof
  180. )
  181. def restart(self):
  182. print(self,"reset()")
  183. self.pos = 0
  184. self.restart_t = time.time()
  185. def init(self):
  186. print("---- ---- Vopen.init()",[self.fname,self._video_nr])
  187. print(PLAYLIST)
  188. self.time = 0
  189. self.t_delta = 0
  190. self.t_last = time.time()
  191. self.im = None
  192. self.pos = 0
  193. self.buffer = []
  194. self._init()
  195. self.init_count = 1
  196. def select_video(self,dmx_value):
  197. try:
  198. dmx_value = int(dmx_value/10)
  199. if self._video_nr != dmx_value:
  200. self._video_nr = dmx_value
  201. if self._video_nr >= 25:
  202. self.fname = "cam_"
  203. self.init()
  204. elif self._video_nr < len(PLAYLIST):
  205. self.fname = str(PLAYLIST[self._video_nr])
  206. self.init()
  207. except Exception as e:
  208. print("Vopen.select_video()",dmx_value,e)
  209. def close_cap():
  210. print(dir(self.Rcap)) # = self.cv2.VideoCapture(self.fpath+self.fname)
  211. def _cam_cfg_new(self,fn):
  212. #fn = HOME+'/LibreLight/video_in.txt'
  213. txt = [
  214. "# Example",
  215. "d0 w640 h480 # PAL",
  216. "d0 w800 h600 # SVGA",
  217. "d0 w960 h540 # 540p",
  218. "d0 w1024 h768 # XGA <---",
  219. "d0 w1280 h720 # 720p",
  220. "d0 w1440 h900 # WSXGA+",
  221. "d0 w1600 h900 # WSXGA - Laptop 16:9",
  222. "d0 w1920 h1080 # HD 1080p",
  223. "",
  224. "# config",
  225. "d0 w1024 h768 # XGA",
  226. ]
  227. f = open(fn ,"w")
  228. f.write("\n".join(txt))
  229. f.close()
  230. def _cam_cfg(self):
  231. fn = HOME+'/LibreLight/video_in.txt'
  232. if not os.path.isfile(fn):
  233. self._cam_cfg_new(fn)
  234. f = open(fn ,"r")
  235. lines = f.readlines()
  236. f.close()
  237. cfg = ""
  238. for i in lines:
  239. if i.startswith("d"):
  240. cfg = i
  241. cfg = cfg.split()
  242. capture = scan_capture(name="MiraBox",serial="")
  243. if capture:
  244. print("capture",capture)
  245. try:
  246. cfg.append(capture[0][0])
  247. except Exception as e:
  248. print("except 2992",e)
  249. else:
  250. print("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----")
  251. print("_cam_cfg:",len(cfg),[cfg])
  252. return cfg
  253. def _open_cam(self):
  254. # LIVE VIDEO INPUT UVC DEVICE
  255. cfg = self._cam_cfg()
  256. res = "HD"
  257. #res = "4K"
  258. #res = ""
  259. if res == "HD":
  260. # HD
  261. w = 1920
  262. h = 1080
  263. elif res == "4K":
  264. # 4K
  265. w = 3840
  266. h = 2160
  267. else:
  268. # LOW
  269. w = 1280
  270. h = 720
  271. d = 0 # VIDEO INPUT DEV
  272. for c in cfg:
  273. try:
  274. if c.startswith("d"):
  275. d = int(c[1:])
  276. if c.startswith("h"):
  277. h = int(c[1:])
  278. if c.startswith("w"):
  279. w = int(c[1:])
  280. if c.startswith("video"):
  281. d = int(c[-1])
  282. except Exception as e:
  283. print("Exception", "cam_cfg 878",e)
  284. df = "video{}".format(d)
  285. if os.path.exists("/dev/{}".format(df)):
  286. self.Rcap = self.cv2.VideoCapture(d)
  287. self.Rcap.set(self.cv2.CAP_PROP_FRAME_WIDTH, w)
  288. self.Rcap.set(self.cv2.CAP_PROP_FRAME_HEIGHT, h)
  289. else:
  290. #self.Rcap = None
  291. #self.end = 1
  292. df += "_error"
  293. if self.fname.startswith("cam_"):
  294. self.fname = "cam_{}".format(df)
  295. def _init(self):
  296. print(self)
  297. print("videoplayer.init()",self.fpath,self.fname)
  298. if not os.path.isfile(self.fpath+self.fname):
  299. print()
  300. print("video file does not exits !! >",self.fpath,self.fname)
  301. print()
  302. #exit()
  303. self.Rsuccess = 0
  304. if self.cv2:
  305. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname)
  306. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_GSTREAMER)
  307. #GSTREAMER Assertion fctx->async_lock failed at libavcodec/pthread_frame.c:175
  308. if self.fname.startswith("cam_"):
  309. self._open_cam()
  310. else:
  311. self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG)
  312. #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
  313. print("_init ?",self.Rcap)
  314. #print(dir(self.Rcap))
  315. #print(self.Rcap.getExceptionMode())
  316. print("open ?" ,self.Rcap.isOpened())
  317. self.Rcap.read()
  318. #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
  319. self.Rsuccess = 1
  320. self._read()
  321. def _del(self):
  322. #self.Rcap = self.cv2.VideoCapture(self.fpath+self.fname)
  323. #for i in dir(self.Rcap):
  324. # print(i)
  325. #print()
  326. #for i in dir(self.cv2):
  327. # print(i)
  328. #time.sleep(0.01)
  329. self.buffer = [] #.append(self.img)
  330. self.Rcap.release()
  331. self.Rcap.retrieve()
  332. self.Rcap = None
  333. self.cap = None
  334. del self.Rcap #.release()
  335. #time.sleep(4)
  336. #sys.exit()
  337. #gc.collect()
  338. #import gc
  339. gc.collect()
  340. def _read(self):
  341. success = self.Rsuccess
  342. ok = 0
  343. if success and self.fname:
  344. cap = self.Rcap
  345. #fvs = self.Rfvs
  346. _break = 0
  347. try:
  348. success, img = cap.read()
  349. if not success:
  350. self.Rcap.release()
  351. self.Rcap.retrieve()
  352. self.end = 1
  353. #j = json.dumps(self.buffer) #.append(img)
  354. #f = open("/tmp/buff")
  355. #f.write(j)
  356. #f.close()
  357. return
  358. if self.fps == 0:
  359. self.fps = cap.get(cv2.CAP_PROP_FPS)
  360. img = self.cv2.cvtColor(img, self.cv2.COLOR_BGR2RGB)
  361. if self.fname.startswith("cam_"):
  362. pass
  363. else:
  364. img = self.rescale_frame2(img, 200)
  365. #ret, img = self.cv2.threshold(img, 100, 130, self.cv2.THRESH_BINARY) # treshold
  366. #self.img = self.cv2.Canny(self.img, 100, 200) # kanten
  367. #M = cv2.getPerspectiveTransform(Punkte_A, Punkte_B)
  368. #warped = cv2.warpPerspective(Bild, m, (420,594))
  369. #self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  370. # store frame into buffer list
  371. if self.fname.startswith("cam_"):
  372. self.buffer = [ img ]
  373. else:
  374. self.buffer.append(img)
  375. ok = 1
  376. if len(self.buffer) % 100 == 0:
  377. _id = str(self.__repr__)[-5:-1]
  378. print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
  379. except Exception as e:
  380. print("Excetpion","_init",self,e,end="")
  381. self.success = 1
  382. return ok
  383. def read(self):
  384. pass
  385. def prev(self):
  386. self.pos -= 1
  387. if self.pos < 0:
  388. self.pos = len(self.buffer)-1
  389. if self.pos >= len(self.buffer):
  390. self.pos = len(self.buffer)-1
  391. self.im = self.buffer[int(self.pos)]
  392. def rotateImage(self,image, angle):
  393. if angle in [0,360]:
  394. pass#return image
  395. try:
  396. #print("EE",image.shape)
  397. #print("EE",dir(image))
  398. shape = list(image.shape[1::-1])
  399. bg_shape = shape[:]
  400. delta_x = 0 # + nach rechts
  401. delta_y = 0 # + nach unten
  402. r = []
  403. r.extend(list(range(0-3,0+3)))
  404. r.extend(list(range(180-3,180+3)))
  405. r.extend(list(range(360-3,360+3)))
  406. r2 = []
  407. r2.extend(list(range(90-3,90+3)))
  408. r2.extend(list(range(270-3,270+3)))
  409. if angle in r:
  410. pass
  411. elif 0:#angle in r2:
  412. # bug on 90 and 270 degrees on portrait video !! TODO
  413. #shape = shape[::-1]
  414. #bg_shape = shape[::-1]
  415. #delta_x = (shape[1]-shape[0])/2
  416. bg_shape = bg_shape[::-1]
  417. delta_y = -65 #shape[1]/2 # # + nach unten
  418. delta_x = 30 #shape[1]/2 # # + nach unten
  419. delta_y -= bg_shape[1]/2 - 20
  420. delta_x = bg_shape[0]/2 - 10
  421. pass
  422. else:
  423. if shape[0] > shape[1]:
  424. # landscape video
  425. bg_shape = (shape[0],shape[0])
  426. delta_y = (shape[0]-shape[1])/2
  427. else:
  428. # portrait video
  429. bg_shape = (shape[1],shape[1])
  430. delta_x = (shape[1]-shape[0])/2
  431. center = (shape[0]/2+delta_x,shape[1]/2+delta_y)
  432. image = self.moveImage(image,x=delta_x,y=delta_y,shape=bg_shape)
  433. bg_shape = tuple(bg_shape) # exception ... if angle 0 no tuple !!!
  434. rot_mat = self.cv2.getRotationMatrix2D(center,angle,1.0)
  435. #print("wrapAffine:", image, rot_mat, bg_shape) #,flags=self.cv2.INTER_LINEAR)
  436. #print("wrapAffine:", type(image), type(rot_mat), type(bg_shape) ) #,flags=self.cv2.INTER_LINEAR)
  437. frame = self.cv2.warpAffine(image, rot_mat, bg_shape ) #,flags=self.cv2.INTER_LINEAR)
  438. return frame
  439. except Exception as e:
  440. raise(e)
  441. def moveImage(self,img,x=0,y=0,shape=None):
  442. # Creating a translation matrix
  443. np = numpy
  444. translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
  445. if shape is None:
  446. num_cols,num_rows = img.shape[1::-1]
  447. else:
  448. num_cols,num_rows = shape #[1::-1]
  449. # Image translation
  450. frame = self.cv2.warpAffine(img, translation_matrix, (num_cols,num_rows))
  451. return frame
  452. def rescale_frame2(self,frame, width):
  453. height = int(frame.shape[0]/frame.shape[1] * width )
  454. dim = (width, height)
  455. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  456. def rescale_frame(self,frame, percent=75):
  457. width = int(frame.shape[1] * percent/ 100)
  458. height = int(frame.shape[0] * percent/ 100)
  459. dim = (width, height)
  460. return self.cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
  461. def pause(self):
  462. #print("pause",self.t_last)
  463. t = time.time()
  464. self.t_delta = 0
  465. self.t_last = t
  466. def next_frame(self):
  467. #return 0
  468. if self._run and len(self.buffer) > 30:
  469. t = time.time()
  470. self.t_delta = t-self.t_last
  471. self.pos += self.t_delta*self.fps
  472. self.t_last = t
  473. else:
  474. self.pause()
  475. if self.restart_t > time.time()-0.5:
  476. # prevent flickerung by reset
  477. #self.pause()
  478. self.pos = 5
  479. pass
  480. # restart at the end
  481. if int(self.pos) >= len(self.buffer)-1:
  482. self.pos = 0
  483. self.img = None
  484. if self.buffer:
  485. self._img = self.buffer[int(self.pos)]
  486. self.img = self._img
  487. def next(self):
  488. try:
  489. self.next_frame()
  490. if self.img is None:
  491. return
  492. #self.img = self.cv2.cvtColor(self.img, self.cv2.COLOR_BGR2RGB)
  493. self.img = self.rescale_frame(self.img, percent=self.scale)
  494. # rotate frame BUG: x,y offset ??? !!!
  495. self.img = self.rotateImage(self.img, self.angle)
  496. #print(sys.getsizeof(self.img))
  497. self.shape = self.img.shape[1::-1]
  498. if len(self.buffer) % 100 == 0:
  499. _id = str(self.__repr__)[-5:-1]
  500. print("next",_id)
  501. # add DIMER to videplayer
  502. self.cv2.normalize(self.img, self.img, 0, self.dim, self.cv2.NORM_MINMAX)
  503. img = self.img
  504. if img is None:
  505. return
  506. self.im = None
  507. self.im = pygame.image.frombuffer(img.tobytes(), self.shape, "RGB")
  508. #self.next_frame()
  509. except AttributeError as e:
  510. time.sleep(.05)
  511. #if self.init_count % 100 == 0:
  512. print("except 776",e)
  513. #self.init()
  514. except Exception as e:
  515. print("except 756",e)
  516. print(traceback.format_exc())
  517. print(sys.exc_info()[2])
  518. print()
  519. def draw(self,wn=None):
  520. if self.dim <= 1:
  521. return
  522. if self.scale < 255*.05:
  523. self.scale = 255*0.05
  524. # draw video background box
  525. __xw = int(self.shape_x*self.scale/255)
  526. __yw = int(self.shape_y*self.scale/255)
  527. #xx ,yy = (370,235)
  528. xx = self.shape_x #= xx
  529. yy = self.shape_y #= yy
  530. try:
  531. xx ,yy = self.im.get_size()[:2]
  532. self.shape_x = xx
  533. self.shape_y = yy
  534. except AttributeError as e:
  535. pass
  536. xx = int(xx*self.scale/255)
  537. yy = int(yy*self.scale/255)
  538. __xw = int(xx) #*self.scale/255)
  539. __yw = int(yy) #*self.scale/255)
  540. yellow = [105,50,0]
  541. yellow[0] = int(yellow[0]*self.dim/255)
  542. yellow[1] = int(yellow[1]*self.dim/255)
  543. yellow[2] = int(yellow[2]*self.dim/255)
  544. #print(yellow)
  545. if self.fname.startswith("cam_"):
  546. #if 10: #corner left up
  547. pygame.draw.rect(wn,yellow,[self.x,self.y,__xw,__yw])
  548. pygame.draw.rect(wn,[25,20,20],[self.x+1,self.y+1,__xw-2,__yw-2])
  549. pygame.draw.line(wn,yellow,[self.x+2,self.y+2],[self.x+__xw-4,self.y+__yw-4])
  550. pygame.draw.line(wn,yellow,[self.x+__xw-4,self.y+2],[self.x+2,self.y+__yw-4])
  551. elif 1: #corner left up
  552. p1 = [self.x+2,self.y+2]
  553. p2 = [self.x+__xw-4,self.y+__yw-4]
  554. p3 = [self.x+__xw-4,self.y+2]
  555. p4 = [self.x+2,self.y+__yw-4]
  556. p1 = [self.x,self.y]
  557. p2 = [self.x+__xw,self.y+__yw]
  558. p3 = [self.x+__xw,self.y]
  559. p4 = [self.x,self.y+__yw]
  560. hx=int(xx/2)
  561. hy=int(yy/2)
  562. #print(hx,hy,xx,yy) #,ratio)
  563. #print("-")
  564. #print(sys.getsizeof(self.buffer))
  565. #print(sys.getsizeof(self))
  566. #print(sys.getsizeof(self.Rcap))
  567. #print(dir(self.Rcap))
  568. p1 = [self.x-hx,self.y-hy]
  569. p2 = [self.x+__xw-hx,self.y+__yw-hy]
  570. p3 = [self.x+__xw-hx,self.y-hy]
  571. p4 = [self.x-hx,self.y+__yw-hy]
  572. pygame.draw.rect(wn,yellow ,[p1[0] ,p1[1] ,__xw ,__yw])
  573. pygame.draw.rect(wn,[25,20,20],[p1[0]+1,p1[1]+1,__xw-2,__yw-2])
  574. pygame.draw.line(wn,yellow ,p1 ,p2)
  575. pygame.draw.line(wn,yellow ,p3 ,p4)
  576. if 0: #corner right down
  577. pygame.draw.rect(wn,yellow,[self.x-__xw,self.y-__yw,__xw,__yw])
  578. pygame.draw.rect(wn,[25,20,20],[self.x+1-__xw,self.y+1-__yw,__xw-2,__yw-2])
  579. pygame.draw.line(wn,yellow,[self.x+2-__xw,self.y+2-__yw],[self.x+__xw-4-__xw,self.y+__yw-4-__yw])
  580. pygame.draw.line(wn,yellow,[self.x+__xw-4-__xw,self.y+2-__yw],[self.x+2-__xw,self.y+__yw-4-__yw])
  581. #pygame.draw.line(wn,yellow,[self.x,self.y],[self.x,self.y+300])
  582. #pygame.draw.line(wn,yellow,[self.x,self.y],[self.x+300,self.y])
  583. pz = 0
  584. #txt = "FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  585. #txt = txt.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  586. #if self.end:
  587. # fr = font15.render(txt,1, (0,255,0))
  588. #else:
  589. # fr = font15.render(txt,1, (255,0,0))
  590. #wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  591. if self.success and wn and self.im: # is not None:
  592. #wn.blit(self.im, (int(self.x), int(self.y)))
  593. #wn.blit(self.im, (int(self.x-__xw), int(self.y-__yw)))
  594. #xx ,yy = self._img.shape[:2]
  595. xx ,yy = self.im.get_size()[:2]
  596. #print(xx,yy)
  597. #wn.blit(self.im, (int(self.x+xx/2), int(self.y+yy/2)))
  598. if self.fname.startswith("cam_"):
  599. wn.blit(self.im, (int(self.x), int(self.y)))
  600. else:
  601. wn.blit(self.im, (int(self.x-xx/2), int(self.y-yy/2)))
  602. def overlay(self,wn=None,mode="x"):
  603. # overlay
  604. pygame.draw.rect(wn,[255,200,0],[5,MAIN_SIZE[1]-(self._id+1)*35,300,28])
  605. font15 = pygame.font.SysFont("freemonobold",17)
  606. pz = 0
  607. if self.end:
  608. rgb = [ 100,255,100]
  609. else:
  610. rgb = [255,100,0]
  611. pygame.draw.rect(wn,rgb,[220,MAIN_SIZE[1]-(self._id+1)*35,80,13])
  612. _line = "error no _line"
  613. _line ="FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f}"
  614. if self.fps == 0: # check if div zerro
  615. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(-1),pz )
  616. else:
  617. pz = (len(self.buffer)/self.fps)
  618. _line = _line.format(self.fps,int(self.pos),len(self.buffer),(self.pos/self.fps),pz )
  619. fr = font15.render(_line ,1, (0,0,0))
  620. wn.blit(fr,(10,MAIN_SIZE[1]-(self._id+1)*35))
  621. if self._run:
  622. mode = "run"
  623. else:
  624. mode = "pause"
  625. fr = font15.render(" {} {} >:{} ".format(self._id+1,self._video_nr,mode) ,1, (0,0,0))
  626. wn.blit(fr,(3,MAIN_SIZE[1]-(self._id+1)*35+15))
  627. fr = font15.render("{}".format(self.fname) ,1, (0,0,0))
  628. wn.blit(fr,(70,MAIN_SIZE[1]-(self._id+1)*35+15))
  629. Vopen = Vopen
  630. VIDEO = []
  631. videoplayer=[]
  632. cv2 = None
  633. FileVideoStream = None
  634. _vid = 0
  635. if type(options.videoplayer) is str:
  636. try:
  637. import cv2
  638. except Exception as e:
  639. print("Except Import:",e)
  640. try:
  641. # faster video reading ... ???
  642. from imutils.video import FileVideoStream
  643. except Exception as e:
  644. print("Except Import:",e)
  645. max_videoplayer = 4
  646. dmx_start = options.videoplayer.split(",")
  647. for cdmx in dmx_start:
  648. if len(videoplayer) > max_videoplayer:
  649. print("-- videoplayer max count {} !! break".format(max_videoplayer))
  650. break
  651. print("-- videoplayer dmx:",cdmx)
  652. try:
  653. cdmx = int(cdmx)
  654. videoplayer.append( Vopen(cdmx,_id=_vid) )
  655. _vid += 1
  656. VIDEO.append({"DMX":cdmx,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"VIDEO":"3","RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":">{}<".format(cdmx)})
  657. except Exception as e:
  658. print("EXCEPTION COUNTER INIT ",cdmx)
  659. def loop_videoplayer():
  660. while 1:
  661. _videoplayer = videoplayer[:]
  662. #print(".")
  663. ok = 0
  664. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  665. try:
  666. r = i._read() # read next frame from file
  667. if r:
  668. ok = 1
  669. except Exception as e:
  670. print("EXCEPTION loop_videoplayer ",e)
  671. #time.sleep(0.002)
  672. #time.sleep(1/120)
  673. if ok == 0:
  674. time.sleep(0.1)
  675. else:
  676. time.sleep(0.005)
  677. thread.start_new_thread(loop_videoplayer,())
  678. # ===== ======
  679. #self.fname = PLAYLIST[0]
  680. videoplayer2 = []
  681. def loop2_videoplayer():
  682. while 1:
  683. print()
  684. print()
  685. print()
  686. for i,v in enumerate(PLAYLIST):
  687. try:
  688. video1 = videoplayer2[i]
  689. video1.select_video(i)
  690. except:
  691. vi = Vopen(181,1)
  692. videoplayer2.append( vi )
  693. _videoplayer = videoplayer2[:]
  694. #print(".")
  695. ok = 0
  696. j =0
  697. for i in _videoplayer: #.append( Vopen(cdmx,_id=_vid) )
  698. try:
  699. r = i._read() # read next frame from file
  700. #print(j,len(videoplayer2),i,len(i.buffer))
  701. if r:
  702. print(j,len(videoplayer2),i,len(i.buffer))
  703. ok = 1
  704. except Exception as e:
  705. print("EXCEPTION loop2_videoplayer ",e)
  706. time.sleep(0.002)
  707. j += 1
  708. if ok == 0:
  709. pass#time.sleep(0.01)
  710. else:
  711. pass#time.sleep(0.005)
  712. #thread.start_new_thread(loop2_videoplayer,())
  713. # PARSE COMMANDLINE ARGUMENTS
  714. CFG_IN = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
  715. CFG_OUT = {"name":"CFG_OUT","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300,"on":0}
  716. CFG_BLOCK = {"name":"CFG_BLOCK","size":16,"h-split":2,"v-split":2,"h-count":8,"v-count":8}
  717. p = 16
  718. block = [p,p]
  719. _x = 8
  720. _y = 8
  721. #HD = "0"
  722. if options.mode:
  723. try:
  724. HD = options.mode
  725. p,_x,_y = HD.split(",")
  726. _x = int(_x)
  727. _y = int(_y)
  728. p = int(p)
  729. block = [p,p]
  730. except Exception as e:
  731. print( "Exc",options.mode,e)
  732. if _x < 1:
  733. _x = 1
  734. if _y < 1:
  735. _y = 1
  736. CFG_BLOCK["h-count"] = _x
  737. CFG_BLOCK["v-count"] = _y # int(8*8/_x) #/_x+0.5) #_y
  738. CFG_BLOCK["size"] = p
  739. print( [options.xsplit])
  740. print( [options.ysplit])
  741. try:
  742. if options.xsplit:
  743. CFG_BLOCK["h-split"] = int(options.xsplit)
  744. if options.ysplit:
  745. CFG_BLOCK["v-split"] = int(options.ysplit)
  746. except Exception as e:
  747. print( "Exc",options.mode,e)
  748. print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
  749. print("xy",_x,_y)
  750. print("++++++++++++++++++", p,_x,_y)
  751. _x2 = _x
  752. try:
  753. if options.XX:
  754. pass#_x2 = int(options.XX)
  755. except Exception as e:
  756. print( "Exc",options.mode,e)
  757. print("_x2 , -X",_x2)
  758. # ===== GUI =========
  759. import pygame
  760. import pygame.gfxdraw
  761. import pygame.font
  762. clock = pygame.time.Clock()
  763. os.environ['SDL_VIDEO_WINDOW_POS'] = '%i,%i' % (200,164)
  764. if options.win_pos:
  765. if "," in options.win_pos:
  766. win_pos = options.win_pos.split(",")
  767. try:
  768. WIN_POS = '%i,%i' % (int(win_pos[0]),int(win_pos[1]) )
  769. os.environ['SDL_VIDEO_WINDOW_POS'] = WIN_POS
  770. except Excetpion as e:
  771. print("win_pos",win_pos,e)
  772. os.environ['SDL_VIDEO_CENTERED'] = '0'
  773. pg = pygame
  774. pygame.init()
  775. pygame.mixer.quit()
  776. f = pygame.font.get_fonts()
  777. for i in f:
  778. if "mono" in i.lower():
  779. print(i)
  780. font = pygame.font.SysFont("freemonobold",22)
  781. font10 = pygame.font.SysFont("freemonobold",10)
  782. font12 = pygame.font.SysFont("freemonobold",12)
  783. font15 = pygame.font.SysFont("freemonobold",15)
  784. font40 = pygame.font.SysFont("freemonobold",40)
  785. font80 = pygame.font.SysFont("freemonobold",70)
  786. #font = pygame.font.SysFont(None,30)
  787. fr = font.render("hallo" ,1, (200,0,255))
  788. PIXEL_MAPPING = 0
  789. grid_file = "/tmp/vpu_grid_hd.csv"
  790. text_file = HOME+"/LibreLight/vpu_text_hd.csv"
  791. play_list = "/tmp/vpu_playlist_hd.csv"
  792. play_list = HOME+"/LibreLight/video/" #.format(path)
  793. pm_wy = 0
  794. if options.pixel_mapping:
  795. PIXEL_MAPPING = 1
  796. CFG_OUT["on"] = 1
  797. path = options.pixel_mapping
  798. path = path.replace("/","-")
  799. path = path.replace(".","-")
  800. path = path.replace("\"","-")
  801. path = path.replace("'","-")
  802. grid_file = HOME+"/LibreLight/vpu_grid_hd{}.csv".format(path)
  803. text_file = HOME+"/LibreLight/vpu_text_hd{}.csv".format(path)
  804. play_list = HOME+"/LibreLight/vpu_playlist_hd{}.csv".format(path)
  805. play_list = HOME+"/LibreLight/video/" #.format(path)
  806. #_x = 8
  807. #_y = 8
  808. print(" ",[options.pixel_mapping],"grid_file",grid_file)
  809. #grid_file = HOME+"/LibreLight/vpu_grid_hd.csv"
  810. MAIN_SIZE=(600,500)
  811. try:
  812. if _x < 8 and PIXEL_MAPPING >= 1:
  813. wx = 30+30+block[0] * 8
  814. CFG_IN["w"] = CFG_BLOCK["size"] * 8
  815. else:
  816. wx = 30+30+block[0] * _x
  817. wy = 40+40+block[1] * _y
  818. if type(options.videoplayer) is str:
  819. wy += 150 # video playlist
  820. MAIN_SIZE=(wx,wy)
  821. if PIXEL_MAPPING >= 1:
  822. pm_wy = 11*p #+ p*3
  823. CFG_IN["y1"] += 11*p
  824. MAIN_SIZE=(wx,wy+pm_wy)
  825. except Exception as e:
  826. print("Exception:",e)
  827. CFG_IN["w"] = CFG_BLOCK["size"] * CFG_BLOCK["h-count"]
  828. CFG_IN["h"] = CFG_BLOCK["size"] * CFG_BLOCK["v-count"]
  829. CFG_OUT["w"] = CFG_BLOCK["size"] * 8
  830. CFG_OUT["h"] = CFG_BLOCK["size"] * 8
  831. def CFG_CALC_P(CFG):
  832. CFG["x2"] = CFG["x1"]+CFG["w"]
  833. CFG["y2"] = CFG["y1"]+CFG["h"]
  834. CFG["p1"] = [CFG["x1"] ,CFG["y1"]]
  835. CFG["p2"] = [CFG["x2"] ,CFG["y2"]]
  836. print("CFG",CFG)
  837. # ?
  838. #CFG_IN["w"] = int(CFG_BLOCK["v-count"]-2) * CFG_BLOCK["size"]
  839. CFG_CALC_P(CFG_IN)
  840. CFG_CALC_P(CFG_OUT)
  841. print("CFG_BLOCK",CFG_BLOCK)
  842. print()
  843. print()
  844. print()
  845. print()
  846. print()
  847. window = pygame.display.set_mode(MAIN_SIZE,pg.RESIZABLE)#,32)#,pygame.FULLSCREEN) #x left->right ,y top-> bottom
  848. #if options.title:
  849. pg.display.set_caption('LibreLight VPU-{}'.format(options.title))
  850. #else:
  851. # pg.display.set_caption('LibreLight VPU-SCREEN')
  852. class Fix():
  853. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  854. #print("Fix",_id)
  855. self._id = _id
  856. self.dmx = (_id-1) * ch +1 #dmx
  857. self.univ = univ
  858. self.ch = ch
  859. self.pos = pos
  860. self.rgb = [0,0,0]
  861. self.block = block #[10,10]
  862. self.x = pos[0]
  863. self.y = pos[1]
  864. self.strobo = time.time()
  865. self.bmp = 250
  866. self.sub_fix = []
  867. sub_block =[block[0]/CFG_BLOCK["h-split"],block[1]/CFG_BLOCK["v-split"]]
  868. if _id <= 0: #exit
  869. return
  870. spalte = (_id-1)%_y +1
  871. zeile = int((_id-1)/_x2) #+1
  872. #zeile = zeile*_x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  873. add_row = _x*CFG_BLOCK["h-split"]*CFG_BLOCK["v-split"]
  874. #zeile 1
  875. sid = (_id-1)*2 + zeile*CFG_BLOCK["h-split"]*_x2
  876. #for i in range(1,CFG_BLOCK["h-split"]):
  877. sid = sid+1
  878. #sid = zeile
  879. sub_pos= [pos[0]*block[0],pos[1]*block[1]]
  880. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  881. self.sub_fix.append(sub_fix)
  882. sid = sid+1
  883. #sid = zeile
  884. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]]
  885. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  886. self.sub_fix.append(sub_fix)
  887. #zeile 2
  888. sid = (_id-1)*2+1 + _x2*CFG_BLOCK["h-split"] + zeile*CFG_BLOCK["h-split"]*_x2 # int(add_row)
  889. #sid = sid+1
  890. #sid = CFG_BLOCK["h-split"]
  891. sub_pos= [pos[0]*block[0],pos[1]*block[1]+block[1]/2]
  892. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  893. self.sub_fix.append(sub_fix)
  894. #sid = sid+1
  895. sid = sid+1
  896. sub_pos= [pos[0]*block[0]+block[0]/2,pos[1]*block[1]+block[1]/2]
  897. sub_fix = SubFix(sid,sub_pos,sub_block,univ,dmx,ch)
  898. self.sub_fix.append(sub_fix)
  899. def calc(self,data):
  900. _rgb = [0,255,0]
  901. return _rgb
  902. def sub_calc(self,data):
  903. _rgb = [0,255,0]
  904. for sub_fix in self.sub_fix:
  905. sub_fix.block = self.block[:]
  906. _rgb = sub_fix.calc(data)
  907. return _rgb
  908. def POS(self,x=0,y=0,a=0,b=0):
  909. A = (self.pos[0])*self.block[0]
  910. B = (self.pos[1])*self.block[1]
  911. C = self.block[0]-a
  912. D = self.block[1]-b
  913. return [x+A,y+B,C,D]
  914. def subPOS(self,x=0,y=0,a=0,b=0):
  915. __out = []
  916. for sub_fix in self.sub_fix:
  917. __out.append( sub_fix.POS(x,y,a,b) )
  918. return __out
  919. class SubFix():
  920. def __init__(self,_id,pos,block=[16,16],univ=0,dmx=0,ch=4):
  921. #print("Fix",_id)
  922. self._id = _id
  923. self.dmx = (_id-1) * ch +1 #dmx
  924. self.univ = univ
  925. self.ch = ch
  926. self.pos = pos
  927. self.rgb = [0,0,40]
  928. self.block = block #[10,10]
  929. self.x = pos[0]
  930. self.y = pos[1]
  931. self.strobo = time.time()
  932. self.bmp = 250
  933. def calc(self,data):
  934. #return [130,30,20]
  935. dmx_sub = [30]*10
  936. #print(dmx_sub)
  937. dmx = self.dmx -1
  938. _dmx_sub = []
  939. if self.dmx >= 0:
  940. dmx = rDMX(self.univ,self.dmx)-1
  941. if dmx+self.ch < len(data):
  942. _dmx_sub = data[dmx:dmx+self.ch]
  943. if _dmx_sub:
  944. dmx_sub = _dmx_sub
  945. #print(dmx_sub)
  946. dim = dmx_sub[0]/255
  947. #print("dmx",dmx,dmx_sub)
  948. r = dmx_sub[1]*dim
  949. g = dmx_sub[2]*dim
  950. b = dmx_sub[3]*dim
  951. r = int(r)
  952. g = int(g)
  953. b = int(b)
  954. self.rgb = [r,g,b]
  955. return self.rgb
  956. def POS(self,x=0,y=0,a=0,b=0):
  957. A = (self.pos[0]) #+self.block[0]
  958. B = (self.pos[1]) #+self.block[1]
  959. C = self.block[0]-a
  960. D = self.block[1]-b
  961. if NR:
  962. C-=1
  963. D-=1
  964. return [int(x+A),int(y+B),int(C),int(D)]
  965. class POINTER():
  966. def __init__(self):
  967. self.pos = [0,0,0,0]
  968. self.on = 0
  969. self.rgb = [0,100,10]
  970. self._x = 0
  971. self._y = 0
  972. self.x = 0
  973. self.y = 0
  974. self.fix = Fix(0 ,[999,999],[16,16],0,0,0)
  975. def row_move(self,x,y):
  976. self._x = x
  977. self._y = y
  978. def move(self,pos):
  979. self.pos = pos
  980. self.on = 1
  981. def cross(self,x,y):
  982. self.x = x
  983. self.y = y
  984. def draw(self,x,y):
  985. pos = self.pos[:]
  986. #print("draw",x,y,pos)
  987. pos[0] += x
  988. pos[1] += y
  989. fix_x= self.fix.x
  990. fix_y= self.fix.y +y
  991. #print("draw",x,y,pos)
  992. if self.on:
  993. pygame.draw.rect(window,self.rgb,pos)
  994. #pygame.draw.line(window,self.rgb, (pos[0],pos[1]) , (pos[0]+100,pos[1]) )
  995. # mouse grid posision
  996. fr = font15.render("{}/{}".format(fix_x+1,fix_y) ,1, (200,200,200))
  997. _nr = fix_y * _x + fix_x +1
  998. #fr = font15.render("{:02} {}/{}".format(_nr, fix_x+1,fix_y+1 ) ,1, (200,200,200))
  999. fr = font15.render("{:02}".format(_nr ) ,1, (200,200,200))
  1000. window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1001. window.blit(fr,(130,1))
  1002. # fix pos
  1003. txt=str(pos) #"[0, 0, 0, 0]"
  1004. fr = font15.render(txt ,1, (200,200,200))
  1005. #window.blit(fr,(pos[0]+2,pos[1]+2 ))
  1006. window.blit(fr,(10,1))
  1007. # univers
  1008. #fr = font15.render("{:02}:{:03}".format(fix.univ,fix.dmx) ,1, (200,200,200))
  1009. #window.blit(fr,(300,10))
  1010. # pointer
  1011. fr = font15.render("X:{:03}".format(self._x) ,1, (200,200,200))
  1012. window.blit(fr,(10,30))
  1013. fr = font15.render("Y:{:03}".format(self._y) ,1, (200,200,200))
  1014. window.blit(fr,(10,40))
  1015. # crosshair
  1016. self.rgb = [0,0,200]
  1017. pygame.draw.line(window,self.rgb, (self.x-p,self.y) , (self.x-2,self.y),4 )
  1018. pygame.draw.line(window,self.rgb, (self.x,self.y-p) , (self.x,self.y-2),4 )
  1019. self.rgb = [0,200,0]
  1020. pygame.draw.line(window,self.rgb, (self.x+2,self.y) , (self.x+p,self.y),4 )
  1021. pygame.draw.line(window,self.rgb, (self.x,self.y+2) , (self.x,self.y+p),4 )
  1022. self.rgb = [200,0,0]
  1023. pointer = POINTER()
  1024. NR = 0
  1025. running = True
  1026. def event():
  1027. global NR,running
  1028. for event in pygame.event.get():
  1029. #print(event.dict)
  1030. _button = None
  1031. if "button" in event.dict:
  1032. _button = event.dict["button"]
  1033. _state = None
  1034. if "state" in event.dict:
  1035. _state = event.state
  1036. _key = None
  1037. if "key" in event.dict:
  1038. _key = event.key
  1039. _pos = None
  1040. if "pos" in event.dict:
  1041. _pos = event.pos
  1042. _type = None
  1043. if "type" in event.dict:
  1044. _type = event.type
  1045. _type = event.type
  1046. _mod = None
  1047. if "mod" in event.dict:
  1048. _mod = event.mod
  1049. if 0:
  1050. print( " ")
  1051. print( "{:.02f}".format( time.time() - START ))
  1052. print("button -",_button,end="\t| ")
  1053. #print("state -",_state)
  1054. print("pos -",_pos)
  1055. print("type -",_type, end="\t| ")
  1056. print("key -",_key)
  1057. print("mod -",_mod)
  1058. try:
  1059. if _type == 5:
  1060. if _button == 1:
  1061. NR += 1
  1062. if NR > 1:
  1063. NR = 0
  1064. if _button == 3:
  1065. NR -= 1
  1066. if NR < 0:
  1067. NR = 1
  1068. if _pos:
  1069. posA = _pos
  1070. fix = find_pix(_pos[0]-40,_pos[1]-60+pm_wy)
  1071. if fix:
  1072. pos = fix.POS(40,60+pm_wy)
  1073. rgb = [0,0,0]
  1074. pointer.move(pos)
  1075. pointer.fix = fix
  1076. else:
  1077. pointer.on = 0
  1078. pointer.row_move(_pos[0],_pos[1])
  1079. pointer.cross(_pos[0],_pos[1])
  1080. if event.type == pygame.VIDEORESIZE:
  1081. window = pygame.display.set_mode((event.w, event.h), pygame.RESIZABLE)
  1082. except Exception as e:
  1083. print(e)
  1084. if event.type==pygame.QUIT:
  1085. running=False
  1086. fps = 0
  1087. fps2 = 0
  1088. frame = 0
  1089. frame2 = 0
  1090. frame_t = time.time()
  1091. frame2_t = time.time()
  1092. IP = "yyy"
  1093. def draw_overlay():
  1094. global fps,fps2
  1095. fr = font15.render("DMX-FPS:{}".format(fps) ,1, (200,0,255))
  1096. window.blit(fr,(10,10))
  1097. fr = font15.render("GUI-FPS :{}".format(fps2) ,1, (200,0,255))
  1098. window.blit(fr,(10,20))
  1099. fr = font.render("ip:{}".format(IP) ,1, (200,0,255))
  1100. window.blit(fr,(90,10))
  1101. def draw_frame(window,rgb,p1,p2,offset=0):
  1102. o = offset
  1103. _p1 = [p1[0]-o,p1[1]-o]
  1104. _p2 = [p1[0]-o,p2[1]+o]
  1105. pygame.draw.line(window,rgb,_p1,_p2) # left
  1106. _p1 = [p2[0]+o,p2[1]+o]
  1107. pygame.draw.line(window,rgb,_p1,_p2) # bottom
  1108. _p2 = [p2[0]+o,p1[1]-o]
  1109. pygame.draw.line(window,rgb,_p1,_p2) # right
  1110. _p1 = [p1[0]-o,p1[1]-o]
  1111. pygame.draw.line(window,rgb,_p1,_p2) # top
  1112. def calc_fps():
  1113. global fps,frame,frame_t
  1114. t = time.time()
  1115. if frame_t+1 < t:
  1116. fps = frame #frame_t- t #frame
  1117. frame = 1
  1118. frame_t = time.time()
  1119. def calc_fps2():
  1120. global fps2,frame2,frame2_t
  1121. t = time.time()
  1122. if frame2_t+0.1 < t:
  1123. fps2 = frame2*10 #frame_t- t #frame
  1124. frame2 = 1
  1125. frame2_t = t #time.time()
  1126. TEXT_BLOCK = []
  1127. def _create_text_block():
  1128. print("======== CREATE NEW TEXT FILE !!",text_file)
  1129. f = open(text_file,"w")
  1130. for i in range(10):
  1131. f.write("TEXT {}\n".format(i+1))
  1132. f.close()
  1133. def open_text_block():
  1134. print("======== OPEN TEXT FILE !!",text_file)
  1135. _lines = []
  1136. try:
  1137. f = open(text_file,"r")
  1138. _lines = f.readlines()
  1139. f.close()
  1140. except FileNotFoundError as e:
  1141. print("TEXT",e)
  1142. _create_text_block()
  1143. if len(_lines) <= 0:
  1144. _create_text_block()
  1145. lines = []
  1146. for l in _lines:
  1147. #print(">> ",l.strip())
  1148. lines.append(l.strip())
  1149. if len(lines) <= 10:
  1150. for i in range(10-len(lines)):
  1151. lines.append("LINE ERROR")
  1152. return lines
  1153. TEXT_BLOCK = open_text_block()
  1154. TEXT_BLOCK_TIME = time.time()
  1155. # video playlist
  1156. PLAYLIST = []
  1157. def open_playlist():
  1158. print("======== OPEN PLAYLIST DIR !!",play_list)
  1159. if not os.path.isdir(play_list):
  1160. os.system("mkdir -p {}".format(play_list))
  1161. _lines = os.listdir(play_list)
  1162. _lines.sort()
  1163. lines = ['']*25 # first is empty
  1164. i=0
  1165. for l in _lines:
  1166. #print(">> ",l.strip(),len(lines))
  1167. l = l.strip()
  1168. if "_" in l:
  1169. ll = l.split("_",1)
  1170. print(">> ",ll)
  1171. #ll = int(ll)
  1172. try:
  1173. lll = int(ll[0])
  1174. #lines.append(l.strip())
  1175. lines[lll] = l
  1176. except:pass
  1177. if len(lines) <= 10:
  1178. for i in range(10-len(lines)):
  1179. lines.append("")#"LINE ERROR")
  1180. return lines
  1181. PLAYLIST_TIME = time.time()
  1182. PLAYLIST = open_playlist()
  1183. # ===== GUI =========
  1184. #def draw_circle(surface, x, y, radius, color):
  1185. def draw_circle(surface,color, pos, radius):
  1186. x,y=pos
  1187. pygame.gfxdraw.aacircle(surface, int(x), int(y), radius-1, color)
  1188. pygame.gfxdraw.filled_circle(surface, int(x), int(y), radius-1, color)
  1189. def rDMX(univ,dmx):
  1190. return univ*512+dmx
  1191. def generate_grid(mapping=0):
  1192. _log = []
  1193. #if PIXEL_MAPPING:
  1194. # log = open(grid_file,"w")
  1195. head = "i,univ,dmx,x,y,ch\n"
  1196. head = "i,univ,dmx,ch\n"
  1197. head = "univ,dmx,x,y,ch\n"
  1198. head = "nr,id,info\n"
  1199. print("csv:",head)
  1200. #if PIXEL_MAPPING:
  1201. # log.write(head)
  1202. _log.append(head)
  1203. dmx = 1-1
  1204. ch = 4
  1205. y=0
  1206. x=0
  1207. #for i in range((_y)*(_x)):
  1208. for i in range((8)*(8)):
  1209. #if x > _x and i%_x == 0:
  1210. if x > 8 and i%8 == 0:
  1211. #print("--> -->")
  1212. x=0
  1213. y+=1
  1214. _univ = int(dmx/512)
  1215. _dmx = dmx - (_univ)*512
  1216. pos=[x,y]
  1217. line="{},{},{},{},{},{}\n".format(i+1,_univ,_dmx+1,pos[0],pos[1],ch)
  1218. line="{},{},{},{},{}\n".format(_univ,_dmx+1,x,y,ch)
  1219. line="{},{},x\n".format(i+1,i+1)
  1220. #print("wcsv:",[line])
  1221. #if PIXEL_MAPPING:
  1222. # log.write(line)
  1223. _log.append(line)
  1224. dmx += ch
  1225. x+=1
  1226. if mapping and PIXEL_MAPPING:
  1227. print("CREATE NEW PIXELMAP FILE !!",grid_file)
  1228. log = open(grid_file,"w")
  1229. log.writelines(_log)
  1230. log.close()
  1231. return _log[:] #GRID
  1232. def init_grid(mapping=0,_x=4,_y=4):
  1233. if mapping and PIXEL_MAPPING:
  1234. try:
  1235. log = open(grid_file,"r")
  1236. except:
  1237. generate_grid(mapping=mapping)
  1238. log = open(grid_file,"r")
  1239. lines = log.readlines()
  1240. else:
  1241. lines = generate_grid()
  1242. GRID = []
  1243. y=0
  1244. x=0
  1245. print("CSV header",[lines[0]],[PIXEL_MAPPING])
  1246. for i,line in enumerate(lines[1:]): #exclude first line
  1247. #print("rcsv",[line])
  1248. line = line.strip()
  1249. line = line.split(",") # csv
  1250. if i >= _x and i%_x == 0:
  1251. x=0
  1252. y+=1
  1253. if y >= _y:
  1254. break
  1255. #i = int(line[0])
  1256. _id = int(line[1])
  1257. #univ = int(line[0])
  1258. #dmx = int(line[1])
  1259. #x = int(line[3])
  1260. #y = int(line[4])
  1261. #ch = int(line[4])
  1262. pos = [x,y]
  1263. f = Fix(_id,pos,block) #pos,univ,dmx,ch)
  1264. #f.x = x
  1265. #f.y = y
  1266. #f.block = block
  1267. GRID.append(f)
  1268. x+=1
  1269. #print("y, _y",y,_y)
  1270. return GRID
  1271. def find_pix(x,y):
  1272. global GRID
  1273. for fix in GRID:
  1274. X = 0
  1275. Y = 0
  1276. pos = fix.POS()
  1277. #pos = fix.POS()
  1278. #pos = fix.POS(40,60-pm_wy)
  1279. #pos = fix.POS(x,y+pm_wy)#40,60)
  1280. if x > pos[0] and x < pos[0]+pos[2]:
  1281. X = 1
  1282. if y > pos[1] and y < pos[1]+pos[3]:
  1283. Y = 1
  1284. if X and Y:
  1285. #print(pos,x,y)
  1286. #print("find",X,Y)
  1287. return fix
  1288. GRID = []
  1289. _GRID = []
  1290. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  1291. NR = 0
  1292. START_UNIV=2
  1293. if options.start_univ:
  1294. try:
  1295. START_UNIV=int(options.start_univ)
  1296. except Exception as e:
  1297. print("Exception START UNIV",e)
  1298. gobo_ch=1
  1299. if options.gobo_ch:
  1300. try:
  1301. gobo_ch=int(options.gobo_ch)
  1302. except Exception as e:
  1303. print("Exception gobo_ch",e)
  1304. if gobo_ch <= 0:
  1305. gobo_ch = 1
  1306. def draw_box(pos1,pos2,color=[128,128,128],text=1):
  1307. color = [200,0,0,127]
  1308. if text:
  1309. fr = font15.render("A" ,1, (200,200,200))
  1310. window.blit(fr,pos1)
  1311. fr = font15.render("B" ,1, (200,200,200))
  1312. window.blit(fr,[pos2[0]-10,pos2[1]-10])
  1313. # h unten
  1314. _pos1 = [pos1[0],pos2[1]]
  1315. _pos2 = [pos2[0],pos2[1]]
  1316. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1317. color = [255,255,0,127]
  1318. # h rechts
  1319. _pos1 = [pos2[0],pos1[1]]
  1320. _pos2 = [pos2[0],pos2[1]]
  1321. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1322. color = [0,200,0,127]
  1323. # h links
  1324. _pos1 = [pos1[0],pos1[1]]
  1325. _pos2 = [pos1[0],pos2[1]]
  1326. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1327. color = [0,0,200,127]
  1328. # h oben
  1329. _pos1 = [pos1[0],pos1[1]]
  1330. _pos2 = [pos2[0],pos1[1]]
  1331. pygame.draw.aaline(window,color,_pos1,_pos2,1)
  1332. def grab(x=55,y=55,w=60,h=60):
  1333. # usage
  1334. # sub = grab()
  1335. # window.blit(sub, (500,10))
  1336. crop = None
  1337. rect = pygame.Rect(x, y, w, h)
  1338. try:
  1339. sub = window.subsurface(rect)
  1340. #pixArray = pygame.PixelArray(screen)
  1341. crop = pygame.Surface((w,h))
  1342. crop.blit(sub, (0,0))
  1343. except ValueError as e:
  1344. pass#print("exception",e,"line715")
  1345. #except Exception as e:
  1346. # print("exception",e,"line715")
  1347. #print(rect)
  1348. return crop
  1349. def reshape(_x,_y):
  1350. """reshape LED-WALL Block/Pixel mapping"""
  1351. if PIXEL_MAPPING <= 0:
  1352. return None
  1353. global GRID
  1354. global _GRID
  1355. x = _x
  1356. y = _y
  1357. i = 0
  1358. counter = 0
  1359. z=0
  1360. x_min = 99999
  1361. x_max = 0
  1362. y_min = 99999
  1363. y_max = 0
  1364. # black background for -> output MAP
  1365. pygame.draw.rect(window,[0,0,20],[0,60,600,pm_wy-1])
  1366. pygame.draw.rect(window,[0,0,20],[wx,wy-80,400,pm_wy+10])
  1367. tmp_font = pygame.font.SysFont("freemonobold",int(p*0.8))
  1368. fr = tmp_font.render("OUTPUT" ,1, (255,255,255))
  1369. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p*1.5)))
  1370. window.blit(fr,fr_r)
  1371. fr = tmp_font.render("↑ ↑ MAP ↑ ↑" ,1, (255,255,255))
  1372. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p)))
  1373. window.blit(fr,fr_r)
  1374. fr = tmp_font.render("INPUT" ,1, (255,255,255))
  1375. fr_r = fr.get_rect(center=(x+int(wx/2),int(60+pm_wy-p/2)))
  1376. window.blit(fr,fr_r)
  1377. j = 0
  1378. for fix in _GRID:
  1379. if j >= 8*8: # max output size
  1380. break
  1381. j+=1
  1382. ii = i
  1383. #z= i # helping border offset
  1384. pos = fix.POS(40,60)
  1385. rgb = fix.rgb
  1386. # green
  1387. pygame.draw.rect(window,[0,40,0],pos)
  1388. xposs = [] #None #pos[:]
  1389. for fix2 in GRID:
  1390. if fix._id == fix2._id:
  1391. xposs.append( fix2.POS(40,60) )
  1392. for xpos in xposs:
  1393. sub = grab(xpos[0],xpos[1]+pm_wy,xpos[2],xpos[3])
  1394. if sub:
  1395. if 1:#j <= _x*_y: # max input size
  1396. window.blit(sub, (x+pos[0]+z,y+pos[1]+z))
  1397. else:
  1398. # red
  1399. pygame.draw.rect(window,[40,0,0],pos) #[x+pos[0]+2+z,y+pos[1]+2+z-pm_wy,12,9])
  1400. if xpos[0] < x_min:
  1401. x_min = xpos[0]
  1402. if xpos[0] > x_max:
  1403. x_max += xpos[2]
  1404. if xpos[1] < y_min:
  1405. y_min = xpos[1]
  1406. if xpos[1] > x_max:
  1407. y_max += xpos[3]
  1408. # DRAW FIX NUMBER on TOP
  1409. #apos = pos
  1410. #argb = rgb
  1411. apos = fix.POS(40,60+pm_wy)
  1412. argb = fix.rgb
  1413. # overwrite number overlay
  1414. if NR:
  1415. #pygame.draw.rect(window,[30,40,0],apos)
  1416. pygame.draw.rect(window,[20,40,0],[x+apos[0]+2+z,y+apos[1]+2+z-pm_wy,12,9])
  1417. if NR:# == 2:
  1418. if fix._id != i+1:
  1419. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  1420. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  1421. #print(fix._id,xposs,pos)
  1422. else:
  1423. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1424. window.blit(fr,(x+apos[0]+2+z,y+apos[1]+2+z-pm_wy))
  1425. i += 1
  1426. #print("--#")
  1427. # frame box
  1428. #pygame.draw.box(window,[100,0,0],[x+x_min,y+x_min,x_max+x_min,y_min+y_max])
  1429. pos1= [x+x_min,y+x_min]
  1430. pos2= [x_max+x_min,y_min+y_max]
  1431. #draw_box(pos1,pos2,text=0)
  1432. class Timer():
  1433. def __init__(self,start=120):
  1434. self.start = start
  1435. self.timer = self.start
  1436. self.timer_t = time.time()
  1437. def reset(self):
  1438. self.timer = self.start
  1439. def get(self):
  1440. self.timer -= time.time()-self.timer_t
  1441. self.timer_t = time.time()
  1442. if self.timer <= 0:
  1443. self.reset()
  1444. return self.timer
  1445. def reload_grid():
  1446. print("==== reload_grid")
  1447. global GRID
  1448. global _GRID
  1449. try:
  1450. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1451. _GRID = init_grid(_x=8,_y=8,mapping=1) #init_gird()
  1452. except Exception as e:
  1453. print("Except: grid re init",e)
  1454. VPU_TEXT = []
  1455. def load_vpu_text(nr=0):
  1456. txt = "NONE"
  1457. if not VPU_TEXT:
  1458. pass #create VPU_TEXT_FILE
  1459. if len(VPU_TEXT) > nr:
  1460. txt = VPU_TEXT[0]
  1461. return txt
  1462. grid_counter = time.time()
  1463. def draw_fix_nr(GRID):
  1464. global grid_counter
  1465. i=0
  1466. y=0
  1467. for fix in GRID:
  1468. pos = fix.POS(40,60+pm_wy)
  1469. rgb = fix.rgb
  1470. if NR:
  1471. pygame.draw.rect(window,[0,0,0],[pos[0]+2,pos[1]+2,12,9])
  1472. if fix._id%_x-1 == 0: # line break border
  1473. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+4),(pos[0],pos[1]+pos[3]-4),1)
  1474. pygame.draw.line(window,[255,255,0],(pos[0],pos[1]+int(pos[3]/2)),(pos[0]+int(pos[2]/2),int(pos[1]+pos[3]/2)),1)
  1475. if fix._id%_x == 0: # line break border
  1476. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,pos[1]+4),(pos[0]+pos[2]-1,pos[1]+pos[3]-4),1)
  1477. pygame.draw.line(window,[255,255,255],(pos[0]+pos[2]-1,int(pos[1]+pos[3]/2)),(pos[0]+int(pos[2]/2-1),int(pos[1]+pos[3]/2)),1)
  1478. if grid_counter +5 < time.time():
  1479. grid_counter = time.time()
  1480. reload_grid()
  1481. if fix._id != i+1:
  1482. fr = font15.render("{:02}".format(fix._id) ,1, (255,255,0))
  1483. else:
  1484. fr = font15.render("{:02}".format(fix._id) ,1, (100,100,255))
  1485. window.blit(fr,(pos[0]+2,pos[1]+2))
  1486. i += 1
  1487. def draw_counter(COUNTER):
  1488. for count in COUNTER:
  1489. cpan = 0
  1490. ctilt = 0
  1491. cr=255
  1492. cg=255
  1493. cb=255
  1494. csize=10
  1495. cdim=0
  1496. k = "DIM"
  1497. if k in count:
  1498. cdim = int(count[k])
  1499. k = "RED"
  1500. if k in count:
  1501. cr = int(count[k])
  1502. k = "GREEN"
  1503. if k in count:
  1504. cg = int(count[k])
  1505. k = "BLUE"
  1506. if k in count:
  1507. cb = int(count[k])
  1508. k = "SIZE"
  1509. if k in count:
  1510. csize = int(count[k]) #/60*p)
  1511. if csize < 5:
  1512. csize = 5
  1513. k = "PAN"
  1514. if k in count:
  1515. cpan = int(count[k])/255*(block[0] *(_x))
  1516. #cpan = int(cpan * 1.2)
  1517. cpan = int(cpan)
  1518. k = "TILT"
  1519. if k in count:
  1520. ctilt = int(count[k])/255*(block[1] *(_y))
  1521. #ctilt = int(cpan * 1.2)
  1522. ctilt = int(ctilt)
  1523. ddim = cdim/255
  1524. if "DIM" in count and count["DIM"] > 0:
  1525. tmp_font = pygame.font.SysFont("freemonobold",int(p/100*csize))
  1526. rgb =(int(cr*ddim),int(cg*ddim),int(cb*ddim),cdim)
  1527. _sec = count["_SEC"]
  1528. try:
  1529. _sec = int(count["_SEC"])
  1530. except:
  1531. pass
  1532. if type(_sec) is int:
  1533. #print(_sec)
  1534. if _sec <= 60:
  1535. fr = tmp_font.render("{:0}".format(_sec) ,1, rgb)
  1536. else:
  1537. _xx = time.strftime("%M:%S",time.localtime(_sec))
  1538. #print("_xx",_xx)
  1539. fr = tmp_font.render("{}".format(_xx) ,1, rgb)
  1540. else:
  1541. fr = tmp_font.render("{}".format((count["_SEC"])) ,1, rgb)
  1542. fr_r = fr.get_rect(center=(40+cpan-(block[0]),60+ctilt+pm_wy))
  1543. pygame.draw.rect(window,[0,0,0],fr_r)
  1544. window.blit(fr,fr_r)
  1545. class FADE():
  1546. def __init__(self,sec):
  1547. self.start = time.time()
  1548. self.sec = sec
  1549. self.val
  1550. def next(self):
  1551. pass
  1552. def draw_video(VIDEO):
  1553. global videplayer
  1554. i = 0
  1555. # set DMX-VALUE to videoplayer Object
  1556. for count in VIDEO:
  1557. cpan = 0
  1558. ctilt = 0
  1559. cr=255
  1560. cg=255
  1561. cb=255
  1562. csize=10
  1563. cang=0
  1564. cdim=0
  1565. video1 = videoplayer[i]
  1566. k = "VIDEO"
  1567. if k in count:
  1568. #video1.select_video(count[k])
  1569. play_nr = int(count[k]/10)
  1570. if play_nr != video1._video_nr:
  1571. print( "+ + + + + + + + + + +", play_nr , video1._video_nr ,str(video1))
  1572. _vid = video1._id
  1573. _cdmx = video1.dmx
  1574. del_video1 = video1
  1575. video1 = Vopen(cdmx,_id=_vid)
  1576. video1.select_video(count[k])
  1577. videoplayer[i] = video1
  1578. del_video1._del()
  1579. del del_video1
  1580. k = "DIM"
  1581. if k in count:
  1582. cdim = int(count[k])
  1583. video1.dim = cdim
  1584. #if i == 0:
  1585. # print(i,cdim)
  1586. k = "ANG"
  1587. if k in count:
  1588. cang = int(count[k])
  1589. k = "SIZE"
  1590. if k in count:
  1591. csize = int(count[k]/16*p)
  1592. if csize < 5:
  1593. csize = 5
  1594. k = "PAN"
  1595. cpan_max = block[0] *(_x) #+block[0]
  1596. if k in count:
  1597. if 0:#video1.fname.startswith("cam_"):
  1598. cpan = int(count[k]) #/ 255*cpan_max
  1599. else:
  1600. cpan = int(count[k]) / 255*cpan_max
  1601. cpan = int(cpan)
  1602. k = "TILT"
  1603. ctilt_max = block[1] *(_y) #+block[1]
  1604. if k in count:
  1605. ctilt = int(count[k]) / 255*ctilt_max
  1606. ctilt = int(ctilt)
  1607. k = "_reset"
  1608. if k in count:
  1609. if count[k]:
  1610. count[k] = 0
  1611. video1.restart()
  1612. k = "_RUN"
  1613. if k in count:
  1614. video1._run = count[k]
  1615. video1.pos
  1616. video1.x=40+0+cpan
  1617. video1.y=60+0+pm_wy+ctilt
  1618. video1.scale = int((csize))
  1619. video1.angle = int(360-(cang))
  1620. if cdim:
  1621. video1.next()
  1622. i += 1
  1623. # draw
  1624. i=0
  1625. for count in VIDEO:
  1626. video1 = videoplayer[i]
  1627. video1.draw(window) #,x=0,y=0)
  1628. i+=1
  1629. i=0
  1630. for count in VIDEO:
  1631. video1 = videoplayer[i]
  1632. video1.overlay(window,"run")
  1633. i += 1
  1634. def counter_dmx(COUNTER,dataA):
  1635. for count in COUNTER:
  1636. cDMX=count["DMX"]-1
  1637. try:
  1638. count["DIM"] = dataA[cDMX]
  1639. count["PAN"] = dataA[cDMX+1]
  1640. count["TILT"] = dataA[cDMX+2]
  1641. count["CONTROL"] = dataA[cDMX+3]
  1642. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1643. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1644. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1645. count["_RUN"] = 0
  1646. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1647. count["_RUN"] = 1
  1648. count["SIZE"] = dataA[cDMX+4]
  1649. count["SEC"] = dataA[cDMX+5]
  1650. if count["_RUN"]:
  1651. try:
  1652. count["_SEC"] = int(count["SEC"] - (time.time() - count["_time"]))
  1653. except Exception as e:
  1654. pass
  1655. if type(count["_SEC"]) is int:
  1656. if count["_SEC"] < 0:
  1657. count["_SEC"] = 0
  1658. for ti in range(10):
  1659. #print(ti,(ti+6)*10)
  1660. if count["CONTROL"] >= (ti+6)*10 and count["CONTROL"] < (ti+7)*10:
  1661. count["_SEC"] = "----" #text 1
  1662. try:
  1663. count["_SEC"] = TEXT_BLOCK[ti]
  1664. except Exception as e:
  1665. pass
  1666. if count["CONTROL"] >= 250 and count["CONTROL"] < 256:
  1667. count["_SEC"] = ">{}<".format(cDMX+1)
  1668. count["RED"] = dataA[cDMX+6]
  1669. count["GREEN"] = dataA[cDMX+7]
  1670. count["BLUE"] = dataA[cDMX+8]
  1671. except Exception as e:
  1672. print("EXC FUNC",e,count)
  1673. #print(count)
  1674. def video_dmx(VIDEO,dataA):
  1675. for count in VIDEO:
  1676. cDMX=count["DMX"]-1
  1677. try:
  1678. count["DIM"] = dataA[cDMX]
  1679. count["PAN"] = dataA[cDMX+1]
  1680. count["TILT"] = dataA[cDMX+2]
  1681. count["CONTROL"] = dataA[cDMX+3]
  1682. if count["CONTROL"] >= 10 and count["CONTROL"] < 20:
  1683. count["_reset"] = 1
  1684. if count["CONTROL"] >= 20 and count["CONTROL"] < 30:
  1685. if count["_RUN"] == 1:
  1686. print( "_RUN:0",count["DMX"]-1)
  1687. count["_RUN"] = 0
  1688. if count["CONTROL"] >= 30 and count["CONTROL"] < 40:
  1689. if count["_RUN"] == 0:
  1690. print( "_RUN:1",count["DMX"]-1)
  1691. count["_RUN"] = 1
  1692. count["SIZE"] = dataA[cDMX+4]#*2
  1693. count["SEC"] = dataA[cDMX+5]
  1694. #count["RED"] = dataA[cDMX+6]
  1695. count["VIDEO"] = dataA[cDMX+6]
  1696. #count["GREEN"] = dataA[cDMX+7]
  1697. count["ANG"] = (int(dataA[cDMX+7])/255*360)
  1698. #count["BLUE"] = dataA[cDMX+8]
  1699. except Exception as e:
  1700. print("VIDEOPLAYER EXCEPT FUNC",e,count)
  1701. #print(count)
  1702. def read_dmx_data(ip,ips):
  1703. ip = select_ip(ips,univ=START_UNIV)
  1704. IP = ip
  1705. data = read_dmx(ip)
  1706. ip = select_ip(ips,univ=START_UNIV+1)
  1707. data3 = read_dmx(ip)
  1708. data.extend(data3)
  1709. ip = select_ip(ips,univ=START_UNIV+2)
  1710. data3 = read_dmx(ip)
  1711. data.extend(data3)
  1712. ip = select_ip(ips,univ=START_UNIV+4)
  1713. data3 = read_dmx(ip)
  1714. data.extend(data3)
  1715. #ip = select_ip(ips,univ=START_UNIV+5)
  1716. #data3 = read_dmx(ip)
  1717. #data.extend(data3)
  1718. return data
  1719. def draw_gobo(GRID,data):
  1720. i = 0
  1721. dmx = 1
  1722. h = 1
  1723. v = 1
  1724. for fix in GRID:
  1725. pos = fix.POS(40,60+pm_wy)
  1726. rgb = fix.rgb
  1727. if 1:
  1728. # draw row/col grid number
  1729. if fix.pos[0] == 0:
  1730. fr = font12.render("{}".format(fix.pos[1]+1) ,1, (200,200,200))
  1731. window.blit(fr,(10,pos[1]+3 ))
  1732. if fix.pos[1] == 0:
  1733. fr = font12.render("{}".format(fix.pos[0]+1) ,1, (200,200,200))
  1734. window.blit(fr,(pos[0]+2,35 ))
  1735. pygame.draw.rect(window,rgb,pos)
  1736. # DRAW SUB-FIXTURE
  1737. j = 0
  1738. for subfix in fix.sub_fix:#calc(data):
  1739. subfix.calc(data)
  1740. #fix = subfix
  1741. spos = subfix.POS(40,60+pm_wy)
  1742. srgb = subfix.rgb
  1743. #print(fix.dmx,rgb,pos)
  1744. #pygame.draw.circle(window,rgb,(pos[0]+int(pos[2]/2),pos[1]+int(pos[3]/2)),int(pos[3]/2))
  1745. #FUNC = 0
  1746. if FUNC > 10 and FUNC <= 20: # big dot
  1747. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1748. elif FUNC > 20 and FUNC <= 30:#small dot
  1749. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1750. elif FUNC > 30 and FUNC <= 40:#donut
  1751. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1752. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1753. elif FUNC > 40 and FUNC <= 50: # rec with hole
  1754. pygame.draw.rect(window,srgb,spos)
  1755. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1756. elif FUNC > 50 and FUNC <= 60: # rec with big hole
  1757. pygame.draw.rect(window,srgb,spos)
  1758. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1759. elif FUNC > 60 and FUNC <= 70: # rec with donat
  1760. pygame.draw.rect(window,srgb,spos)
  1761. draw_circle(window,[0,0,0],(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/2))
  1762. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1763. elif FUNC > 70 and FUNC <= 80: # rec boarder
  1764. pygame.draw.rect(window,srgb,[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1765. elif FUNC > 80 and FUNC <= 90: # rec big boarder
  1766. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1767. elif FUNC > 90 and FUNC <= 100: # rec thin line
  1768. pygame.draw.rect(window,srgb,spos)
  1769. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1770. elif FUNC > 100 and FUNC <= 110: # rec big line
  1771. pygame.draw.rect(window,srgb,spos)
  1772. pygame.draw.rect(window,[0,0,0],[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1773. elif FUNC > 110 and FUNC <= 120: # rec with dot
  1774. pygame.draw.rect(window,srgb,spos)
  1775. pygame.draw.rect(window,[0,0,0],[spos[0]+1,spos[1]+1,spos[2]-2,spos[3]-2])
  1776. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1777. elif FUNC > 120 and FUNC <= 130: # rec inline
  1778. pygame.draw.rect(window,srgb,[spos[0]+2,spos[1]+2,spos[2]-4,spos[3]-4])
  1779. pygame.draw.rect(window,[0,0,0],[spos[0]+3,spos[1]+3,spos[2]-6,spos[3]-6])
  1780. elif FUNC > 130 and FUNC <= 140: # 3 dot (heart)
  1781. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)+2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1782. draw_circle(window,srgb,(spos[0]+int(spos[2]/2)-2,spos[1]+int(spos[3]/2)),int(spos[3]/3.5))
  1783. draw_circle(window,srgb,(spos[0]+int(spos[2]/2),spos[1]+int(spos[3]/2)+2),int(spos[3]/3.5))
  1784. else:
  1785. pygame.draw.rect(window,srgb,spos)
  1786. for subfix in fix.sub_fix:#calc(data):
  1787. subfix.calc(data)
  1788. #fix = subfix
  1789. spos = subfix.POS(40,60+pm_wy)
  1790. srgb = subfix.rgb
  1791. # draw row/col grid number
  1792. if subfix.pos[0] == 0:
  1793. fr = font12.render("{}".format(v ) ,1, (200,200,200))
  1794. window.blit(fr,(25,spos[1] ))
  1795. v += 1
  1796. if subfix.pos[1] == 0:
  1797. fr = font12.render("{}".format(1) ,1, (200,200,200))
  1798. fr = font12.render("{}".format(h ) ,1, (200,200,200))
  1799. h+=1
  1800. window.blit(fr,(spos[0],50 ))
  1801. if p >= 40:
  1802. if NR:
  1803. #fr = font15.render("{:02}".format(j+1) ,1, (0,200,255))
  1804. fr = font15.render("{:02}".format(subfix._id) ,1, (250,200,5))
  1805. window.blit(fr,(spos[0]+2,spos[1]+10))
  1806. j += 1
  1807. i += 1
  1808. def frame_area():
  1809. rgb = [255,255,0]
  1810. CFG = CFG_IN
  1811. p1 = CFG_IN["p1"]
  1812. p2 = CFG_IN["p2"]
  1813. draw_frame(window,rgb,p1,p2,offset=2)
  1814. rgb = [255,0,0]
  1815. #draw_frame(window,rgb,p1,p2,offset=4)
  1816. if CFG_OUT["on"]:
  1817. rgb = [255,0,0]
  1818. p1 = CFG_OUT["p1"]
  1819. p2 = CFG_OUT["p2"]
  1820. #pygame.draw.line(window,rgb,p1,p2)
  1821. draw_frame(window,rgb,p1,p2,offset=3)
  1822. ips=[]
  1823. dataA=[]
  1824. data=[]
  1825. def dmx_raw():
  1826. global frame
  1827. frame += 1
  1828. global ips,dataA,data
  1829. ips = read_index()
  1830. # ----
  1831. ip = select_ip(ips,univ=1) # univ 1 gobo
  1832. dataA = read_dmx(ip)
  1833. # ----
  1834. data = read_dmx_data(ip,ips)
  1835. if options.countdown:
  1836. counter_dmx(COUNTER,dataA)
  1837. if len(VIDEO) > 0:
  1838. video_dmx(VIDEO,dataA)
  1839. return ips,dataA,data
  1840. def dmx_loop():
  1841. while 1:
  1842. dmx_raw()
  1843. time.sleep(1/40) # fast high cpu
  1844. #time.sleep(1/25)
  1845. dmx_raw()
  1846. thread.start_new_thread(dmx_loop,())
  1847. t1 = Timer(143)
  1848. time.sleep(0.33)
  1849. t2 = Timer(11)
  1850. count_tilt = 0
  1851. def main():
  1852. global IP
  1853. global GRID
  1854. global _GRID
  1855. global FUNC
  1856. global count_tilt
  1857. global TEXT_BLOCK
  1858. global TEXT_BLOCK_TIME
  1859. global PLAYLIST
  1860. global PLAYLIST_TIME
  1861. global dataA
  1862. global frame2
  1863. GRID = init_grid(_x=_x,_y=_y) #init_gird()
  1864. #GRID = init_grid(_x=8,_y=8) #init_gird()
  1865. print("GRID LEN:",len(GRID))
  1866. s=time.time()
  1867. print("run")
  1868. r = ""
  1869. IP = "xx"
  1870. while running:
  1871. if TEXT_BLOCK_TIME+5 < time.time():
  1872. TEXT_BLOCK = open_text_block()
  1873. TEXT_BLOCK_TIME = time.time()
  1874. if PLAYLIST_TIME+6 < time.time():
  1875. PLAYLIST = open_playlist()
  1876. PLAYLIST_TIME = time.time()
  1877. pygame.display.flip()
  1878. event()
  1879. window.fill((10,0,30))
  1880. calc_fps()
  1881. calc_fps2()
  1882. draw_overlay()
  1883. # GRID loop
  1884. try:
  1885. ddd = 1023 #univ 3 512
  1886. #FUNC = data[ddd]
  1887. FUNC2 = dataA[gobo_ch-1]
  1888. FUNC = FUNC2
  1889. #print("FUNC", FUNC )#:ddd+512])
  1890. #FUNC = 15
  1891. except Exception as e:
  1892. print("EXC FUNC",e)
  1893. draw_gobo(GRID,data)
  1894. # DRAW FIX NUMBER on TOP
  1895. draw_fix_nr(GRID)
  1896. #COUNTER.append({"DMX":31,"DIM":0,"PAN":127,"TILT":127,"CONTROL":0,"SEC":10,"RED":255,"GREEN":255,"BLUE":255,"_time":time.time(),"_RUN":0,"_SEC":0})
  1897. if VIDEO:
  1898. draw_video(VIDEO)
  1899. if options.countdown:
  1900. draw_counter(COUNTER)
  1901. pointer.draw(0,pm_wy) #wy
  1902. spos = [0,0,0,0]
  1903. if PIXEL_MAPPING >= 1:
  1904. reshape(0,0) #start pos
  1905. else:
  1906. reshape(spos[0]+spos[2]+20,10) #start pos
  1907. #reshape(spos[0]+spos[2]+20,10) #start pos
  1908. frame_area()
  1909. pygame.display.flip()
  1910. #pg.time.wait(55)
  1911. #clock.tick(120) # fast hight cpu
  1912. clock.tick(25)
  1913. #time.sleep(1/120)
  1914. if 'SDL_VIDEO_WINDOW_POS' in os.environ:
  1915. del os.environ['SDL_VIDEO_WINDOW_POS'] #= '%i,%i' % (200,164)
  1916. #if 'SDL_VIDEO_CENTERED' in os.environ['SDL_VIDEO_CENTERED']:
  1917. # del os.environ['SDL_VIDEO_CENTERED'] #= '0'
  1918. frame2 += 1
  1919. if __name__ == "__main__":
  1920. main()