소스 검색

add:vpu-media-img 00x-123.jpg and cleanup

micha 3 일 전
부모
커밋
3f290664d6
1개의 변경된 파일138개의 추가작업 그리고 68개의 파일을 삭제
  1. 138 68
      vpu/vpu_live4.py

+ 138 - 68
vpu/vpu_live4.py

@@ -8,6 +8,9 @@ import sys
 import traceback
 import gc
 
+sys.path.insert(0,"/opt/LibreLight/Xdesk/")
+from lib.cprint import cprint 
+
 HOME = os.getenv('HOME')
 
 import _thread as thread
@@ -119,7 +122,7 @@ if options.countdown:
                     }
             COUNTER.append(_tmp)
         except Exception as e:
-            print("EXCEPTION COUNTER INIT ",cdmx)
+            cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
 
 
 
@@ -159,7 +162,6 @@ except:pass
 #import json
 #import pickle
 
-sys.path.insert(0,"/opt/LibreLight/Xdesk/")
 from tool.video_capture import scan_capture #as scan_capture
 
 def _cam_cfg_new(fn):
@@ -200,9 +202,9 @@ def _cam_cfg():
         try:
             cfg.append(capture[0][0])
         except Exception as e:
-            print("except 2992",e)
+            cprint("except 2992",e,color="red")
     else:
-        print("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----")
+        cprint("---- ERROR NO VIDEO CAPTURE DEV - FOUND ----",color="red")
     print("_cam_cfg:",len(cfg),[cfg])
     
     return cfg
@@ -220,8 +222,9 @@ class PixelMedia():
         try:
             self.fname = MEDIALIST[0]
         except Exception as e:
-            print("Exception set video from MEDIALIST 5543:",e)
-        
+            cprint("Exception set video from MEDIALIST 5543:",e,color="red")
+
+        self.img_meta = {}
         self.restart_t = time.time()
         self.fps = 0
         self.scale = 50 #%
@@ -239,7 +242,7 @@ class PixelMedia():
         self.success = 1
         self._run = 0
         self.end = 0
-        self._video_nr = 0
+        self._media_nr = 0
 
         self._stop = 0 # fix buffer-overflow ... ?? opencv
 
@@ -260,7 +263,7 @@ class PixelMedia():
         self.restart_t = time.time()
 
     def init(self):
-        print("---- ---- PixelMedia.init()",[self.fname,self._video_nr])
+        print("---- ---- PixelMedia.init()",[self.fname,self._media_nr])
         print(MEDIALIST)
         self.time = 0
         self.t_delta = 0 
@@ -275,18 +278,18 @@ class PixelMedia():
         try:
             dmx_value = int(dmx_value/10)
 
-            if self._video_nr != dmx_value:
-                self._video_nr = dmx_value
+            if self._media_nr != dmx_value:
+                self._media_nr = dmx_value
 
-                if self._video_nr >= 25:
+                if self._media_nr >= 25:
                     self.fname = "cam_"
                     self.init()
-                elif self._video_nr < len(MEDIALIST):
-                    self.fname = str(MEDIALIST[self._video_nr])
+                elif self._media_nr < len(MEDIALIST):
+                    self.fname = str(MEDIALIST[self._media_nr])
                     self.init()
-
-        except Exception as e:
-            print("PixelMedia.select_video()",dmx_value,e)
+        finally:pass
+        #except Exception as e:
+        #    cprint("PixelMedia.select_video()",dmx_value,e,color="red")
 
     def close_cap():
         print(dir(self.Rcap)) # = cv2.VideoCapture(self.fpath+self.fname)
@@ -328,7 +331,7 @@ class PixelMedia():
                 if c.startswith("video"):
                     d = int(c[-1])
             except Exception as e:
-                print("Exception", "cam_cfg 878",e)
+                cprint("Exception", "cam_cfg 878",e,color="red")
 
         df = "video{}".format(d)
         if self._stop:
@@ -357,25 +360,40 @@ class PixelMedia():
             #exit()
     
         self.Rsuccess = 0
-        if cv2:
-            if self._stop:
-                return
 
-            if self.fname.startswith("cam_"): 
-                self._open_cam()
-            else:
-                self.Rcap = cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG) 
-                #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
+        if not cv2:
+            return
 
-            print("_init ?",self.Rcap)
-            print("open ?" ,self.Rcap.isOpened())
-            self.Rcap.read()
+        if self._stop:
+            return
 
-            #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
-            self.Rsuccess = 1
-            self._read()
+        if self.fname.endswith(".jpg"): 
+            self.img_meta = create_img_meta()        
+            self.img_meta["_fpath"] = self.fpath
+            self.img_meta["fname"] = self.fname
+            img_open(self.img_meta)
+            self.buffer = [1,2,3]
+            return
+
+        if self.fname.startswith("cam_"): 
+            self._open_cam()
+            self.img_meta = {}
+        else:
+            self.Rcap = cv2.VideoCapture(self.fpath+self.fname, cv2.CAP_FFMPEG) 
+            #FFMPEG malloc(): unsorted double linked list corrupted ... Abgebrochen
+            self.img_meta = {}
+
+        print("_init ?",self.Rcap)
+        print("open ?" ,self.Rcap.isOpened())
+        self.Rcap.read()
+
+        #self.Rfvs = FileVideoStream(self.fpath+self.fname).start()
+        self.Rsuccess = 1
+        self._read()
 
     def _del(self):
+        if self.img_meta:
+            return
         self._stop = 1
 
         self.buffer = [] #.append(self.img)
@@ -389,6 +407,8 @@ class PixelMedia():
 
 
     def _read(self):
+        if self.img_meta:
+            return
         success = self.Rsuccess
         ok = 0
         if success and self.fname:
@@ -433,7 +453,7 @@ class PixelMedia():
                     print(_id,"video read",self.dmx,len(self.buffer),self.fname,"fps",self.fps,self.dim)
 
             except Exception as e:
-                print("Excetpion","_init",self,e,end="")
+                cprint("Exception","_init",self,e,end="",color="red")
         self.success = 1
         return ok
 
@@ -449,6 +469,8 @@ class PixelMedia():
         self.im = self.buffer[int(self.pos)]
 
     def rotateImage(self,image, angle):
+        if self.img_meta:
+            return
         if angle in [0,360]:
             pass#return image
         try:
@@ -498,6 +520,8 @@ class PixelMedia():
         except Exception as e:
             raise(e)
     def moveImage(self,img,x=0,y=0,shape=None):
+        if self.img_meta:
+            return
         # Creating a translation matrix
         np = numpy
         translation_matrix = np.float32([ [1,0,x], [0,1,y] ])
@@ -511,24 +535,32 @@ class PixelMedia():
         return frame
 
     def rescale_frame2(self,frame, width):
+        if self.img_meta:
+            return
         height = int(frame.shape[0]/frame.shape[1] * width )
         dim = (width, height)
         #return cv2.resize(frame, dim, interpolation =cv2.INTER_AREA)
         return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
 
     def rescale_frame(self,frame, percent=75):
+        if self.img_meta:
+            return
         width  = int(frame.shape[1] * percent/ 100)
         height = int(frame.shape[0] * percent/ 100)
         dim = (width, height)
         return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
 
     def pause(self):
+        if self.img_meta:
+            return
         #print("pause",self.t_last)
         t = time.time()
         self.t_delta = 0 
         self.t_last = t
 
     def next_frame(self):
+        if self.img_meta:
+            return
         #return 0
         
         if self._run and len(self.buffer) > 30:
@@ -559,6 +591,9 @@ class PixelMedia():
             return 0
         return 1
     def next(self):
+        if self.img_meta:
+            self.next_frame()
+            return
          
         try:
             self.next_frame()
@@ -592,12 +627,19 @@ class PixelMedia():
             print("except 776",e)
             #self.init()
         except Exception as e:
-            print("except 756",e)
-            print(traceback.format_exc())
-            print(sys.exc_info()[2])
-            print()
+            cprint("except 756",e,color="red")
+            cprint(traceback.format_exc(),color="red")
+            cprint(sys.exc_info()[2],color="red")
+            cprint()
 
     def draw(self,wn=None):
+        if self.img_meta:
+            self.img_meta["pos"] = [self.x,self.y]
+            self.img_meta["scale"] = self.scale #= 50 #%
+            self.img_meta["dim"] = self.dim #255
+            self.img_meta["ang"] = self.angle # 0
+            img_draw_raw(self.img_meta)
+            return
         
         if self.dim <= 1:
             return
@@ -684,14 +726,6 @@ class PixelMedia():
         else:
             rgb = [255,100,0]
         pygame.draw.rect(wn,rgb,[220,MAIN_SIZE[1]-(self._id+1)*35,260,13])
-        #self.scale = 50 #%
-        #self.angle = 0 #%
-        #self.dmx=dmx
-        #if self._id >= 250:
-        #    self.fname = "cam_"
-        #self.dim = 0
-        #self.x = 0
-        #self.y = 0
 
         _line = "error no _line"
         _line ="FPS:{} F:{:05} von {:05} sec:{:0.02f} von {:0.02f} POS:{:#}:{:#} z:{:#} d:{:#}"
@@ -708,7 +742,7 @@ class PixelMedia():
             mode = "run"
         else:
             mode = "pause"
-        fr = font15.render(" {} {} >:{} ".format(self._id+1,self._video_nr,mode) ,1, (0,0,0))
+        fr = font15.render(" {} {} >:{} ".format(self._id+1,self._media_nr,mode) ,1, (0,0,0))
         wn.blit(fr,(3,MAIN_SIZE[1]-(self._id+1)*35+15))
 
         fr = font15.render("{}".format(self.fname) ,1, (0,0,0))
@@ -726,12 +760,12 @@ if type(options.videoplayer) is str:
     try:
         import cv2
     except Exception as e:
-        print("Except Import:",e)
+        cprint("Except Import:",e,color="red")
     try:
         # faster video reading ... ???
         from imutils.video import FileVideoStream
     except Exception as e:
-        print("Except Import:",e)
+        cprint("Except Import:",e,color="red")
 
     max_videoplayer = 4
     dmx_start =  options.videoplayer.split(",")
@@ -752,7 +786,7 @@ if type(options.videoplayer) is str:
                    }
             VIDEO.append(_tmp)
         except Exception as e:
-            print("EXCEPTION COUNTER INIT ",cdmx)
+            cprint("EXCEPTION COUNTER INIT ",cdmx,color="red")
 
 def loop_videoplayer():
     while 1:
@@ -765,7 +799,7 @@ def loop_videoplayer():
                 if r:
                     ok = 1
             except Exception as e:
-                print("EXCEPTION loop_videoplayer ",e)
+                cprint("EXCEPTION loop_videoplayer ",e,color="red")
 
         if ok == 0:
             time.sleep(0.1)
@@ -802,7 +836,7 @@ def loop2_videoplayer():
                     print(j,len(videoplayer2),i,len(i.buffer))
                     ok = 1
             except Exception as e:
-                print("EXCEPTION loop2_videoplayer ",e)
+                cprint("EXCEPTION loop2_videoplayer ",e,color="red")
 
             time.sleep(0.002)
             j += 1
@@ -826,7 +860,7 @@ if options.mode:
 
         block = [p,p]
     except Exception as e:
-        print( "Exc",options.mode,e)
+        cprint( "Exc",options.mode,e,color="red")
 
 # PARSE COMMANDLINE ARGUMENTS
 CFG_IN    = {"name":"CFG_IN","x1":40,"y1":60,"x2":300,"y2":300 ,"w":300,"h":300}
@@ -855,7 +889,7 @@ try:
     if options.ysplit:
         CFG_BLOCK["v-split"] = int(options.ysplit)
 except Exception as e:
-    print( "Exc",options.mode,e)
+    cprint( "Exc",options.mode,e,color="red")
 
 
 print("HD",CFG_BLOCK["h-split"],CFG_BLOCK["v-split"])
@@ -2094,8 +2128,8 @@ def draw_video(VIDEO):
             #video1.select_video(count[k])
 
             play_nr = int(count[k]/10)
-            if play_nr != video1._video_nr:
-                print( "+ + + +  + + + + + + +", play_nr , video1._video_nr ,str(video1))
+            if play_nr != video1._media_nr:
+                print( "+ + + +  + + + + + + +", play_nr , video1._media_nr ,str(video1))
                 _vid  = video1._id
                 _cdmx = video1.dmx
                 del_video1 = video1
@@ -2571,8 +2605,9 @@ def create_img_meta():
     #40 235
     out["pos"] = (0,0) # x,y
     out["ipos"] = (0,0) # x,y
-    out["scale"] = 1
+    out["scale"] = 50
     out["dim"] = 100
+    out["ang"] = 0
     out["_fpath"] = '/opt/LibreLight/Xdesk/icon/'
     out["fname"] = "picker.png"
     out["raw_img"] = ""
@@ -2583,6 +2618,23 @@ def create_img_meta():
 def img_open(img_meta):
     fname = img_meta["_fpath"] +"/"+ img_meta["fname"] #'/opt/LibreLight/Xdesk/icon/picker.png'
     im2 = cv2.imread(fname)
+    width  = int(im2.shape[1])
+    height = int(im2.shape[0])
+    w_max = 480
+    h_max = 320
+    #w_max = 600
+    #h_max = 400
+    if width > w_max:
+        h = int(height*(w_max/width))
+        w = w_max
+        im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
+
+    if height > h_max:
+        w = int(width*(h_max/height))
+        h = h_max
+        if w < w_max:
+            im2 = cv2.resize(im2, (w,h), interpolation =cv2.INTER_LINEAR)
+
     img_meta["raw_img"] = im2
     img_meta["img"] = im2
     return im2
@@ -2592,6 +2644,19 @@ def img_jitter(img_meta):
     r2=random.randint(0,3)
     img_meta["ipos"] = [r1,r2]
 
+def img_rescale_width(img_meta, _width):
+    frame = img_meta["raw_img"]
+    width  = int(frame.shape[1])
+    height = int(frame.shape[0])
+    dim = (width, height)
+    if 1: #width > _width:
+        h = int(height*( width/_width))
+        dim = (_width, h)
+        im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
+    else:
+        im = cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
+    return im
+
 def img_rescale(img_meta, percent=75):
     frame = img_meta["raw_img"]
     width  = int(frame.shape[1] * percent/ 100)
@@ -2599,24 +2664,14 @@ def img_rescale(img_meta, percent=75):
     dim = (width, height)
     return cv2.resize(frame, dim, interpolation =cv2.INTER_LINEAR)
 
-def __img_rescale(img_meta, percent=75):
-    img = img_meta["raw_img"]
-    print(img)
-    print(dir(img))
-    width  = int(img.get_width()  * percent/ 100)
-    height = int(img.get_height() * percent/ 100)
-    dim = (width, height)
-    print(dim)
-    return cv2.resize(img, dim) #, interpolation =cv2.INTER_LINEAR)
-
-def img_draw(img_meta):
 
+def img_draw_inp(img_meta):
     #img_jitter(img_meta)
 
     pos  = img_meta["pos"]
     ipos = img_meta["ipos"]
     im2 = img_meta["img"]
-    im2 = img_rescale(img_meta, percent=175)
+    im2 = img_rescale(img_meta, percent=img_meta["scale"])
 
 
     im2 = cvImageToSurface(im2)
@@ -2624,6 +2679,21 @@ def img_draw(img_meta):
     h2 = int(im2.get_height()/2)
     window.blit(im2, (INPUT_START_POS_X-w2+pos[0]+ipos[0], INPUT_START_POS_Y-h2+pos[1]+ipos[1])) 
 
+def img_draw_raw(img_meta):
+
+    #img_jitter(img_meta)
+
+    pos  = img_meta["pos"]
+    ipos = img_meta["ipos"]
+    im2 = img_meta["img"]
+    #def img_rescale_width(img_meta, _width):
+    im2 = img_rescale(img_meta, percent=img_meta["scale"])
+
+    im2 = cvImageToSurface(im2)
+    w2 = int(im2.get_width()/2)
+    h2 = int(im2.get_height()/2)
+    window.blit(im2, (pos[0]+ipos[0]-w2, pos[1]+ipos[1]-h2)) 
+
 img_meta_a = create_img_meta()
 img_open(img_meta_a)
 
@@ -2694,7 +2764,7 @@ def main():
 
         if VIDEO:
             draw_video(VIDEO)
-            img_draw(img_meta_a)
+            #img_draw(img_meta_a)
 
         if options.countdown:
             draw_counter(COUNTER)