daily pastebin goal
32%
SHARE
TWEET

Untitled

a guest Mar 24th, 2019 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import argparse
  2. import cv2
  3. import numpy
  4.  
  5. helpText = "'Esc' to Quit"
  6.  
  7. def parse_args():
  8.     parser = argparse.ArgumentParser(description="CV2 Video Capture PhotoPlethysmoGram Test")
  9.     parser.add_argument("--vid", dest="video_dev",
  10.                         help="video device # of USB webcam (/dev/video?) [1]",
  11.                         default=0, type=int)
  12.     args = parser.parse_args()
  13.     return args
  14.  
  15. def line(x0, x1, y0, y1, func):
  16.     isSwap = abs(y1 - y0) > abs(x1 - x0)
  17.     if isSwap:
  18.         x0, y0 = y0, x0
  19.         x1, y1 = y1, x1
  20.     if x0 > x1:
  21.         x0, x1 = x1, x0
  22.         y0, y1 = y1, y0
  23.     deltax = x1 - x0
  24.     deltay = abs(y1 - y0)
  25.     error = deltax / 2
  26.     y = y0
  27.     if y0 < y1:
  28.         ystep = 1
  29.     else:
  30.         ystep = -1
  31.     for x in range(x0, x1):
  32.         if isSwap:
  33.             func(y, x)
  34.         else:
  35.             func(x, y)
  36.         error = error - deltay
  37.         if error < 0:
  38.             y = y + ystep
  39.             error = error + deltax
  40.  
  41. def plot_r(x, y):
  42.     frame[int(y), int(x)] = [255,0,0]
  43. def plot_g(x, y):
  44.     frame[int(y), int(x)] = [0,255,0]
  45. def plot_b(x, y):
  46.     frame[int(y), int(x)] = [0,0,255]                
  47. def plot_w(x, y):
  48.     frame[int(y), int(x)] = [255,255,255]                
  49.    
  50. if __name__ == "__main__":
  51.     args = parse_args()
  52.     print("Called with args:")
  53.     print(args)
  54.     cap = cv2.VideoCapture(args.video_dev)
  55.     width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
  56.     height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
  57.     px = int(width /2 )  # float
  58.     py = int(height /2 ) # float
  59.     print(py,px)
  60.    
  61.     drawDataList = []
  62.    
  63.     while(True):
  64.         ret, frame = cap.read()
  65.         if ret:
  66.           cv2.putText(frame, helpText, (11,20), cv2.FONT_HERSHEY_PLAIN, 1.0, (32,32,32), 4, cv2.LINE_AA)
  67.           cv2.putText(frame, helpText, (10,20), cv2.FONT_HERSHEY_PLAIN, 1.0, (240,240,240), 1, cv2.LINE_AA)
  68.           #getPixel = frame[py,px]
  69.           #print (getPixel)
  70.           avg_color_per_row = numpy.average(frame, axis=0)
  71.           avg_color = numpy.average(avg_color_per_row, axis=0)
  72.           #drawDataList.append(frame[py,px])
  73.           drawDataList.append(avg_color)
  74.           if len(drawDataList) > width/4 :
  75.               drawDataList.pop(0)
  76.          
  77.           now_x = 0
  78.           old_x = 0
  79.           old_ry = 0
  80.           old_gy = 0
  81.           old_by = 0
  82.           for p in drawDataList:
  83.               by = int((255-p[0]) * 1.5)
  84.               gy = int((255-p[1]) * 1.5)
  85.               ry = int((255-p[2]) * 1.5)
  86.               if now_x == 0:
  87.                   frame[old_by, now_x] = [255,0,0]
  88.                   frame[old_gy, now_x] = [0,255,0]
  89.                   frame[old_ry, now_x] = [0,0,255]
  90.               else:
  91.                   line(old_x, now_x, old_by, by, plot_r)
  92.                   line(old_x, now_x, old_gy, gy, plot_g)
  93.                   line(old_x, now_x, old_ry, ry, plot_b)
  94.                   line(old_x+1, now_x+1, old_ry+1, ry+1, plot_b)
  95.                   line(old_x+2, now_x+2, old_ry+2, ry+2, plot_w)
  96.                   #dx = now_x - old_x
  97.                   ##dx = 1
  98.                   #dby = by - old_by
  99.                   #dgy = gy - old_gy
  100.                   #dry = ry - old_ry
  101.  
  102.                   #for y in range(old_by, by, 1 if old_by < by else -1):
  103.                   #    x = old_x + (dx * (y - old_by)) / dby
  104.                   #    frame[int(y), int(x)] = [255,0,0]
  105.                   #for y in range(old_gy, gy, 1 if old_gy < gy else -1):
  106.                   #    x = old_x + (dx * (y - old_gy)) / dgy
  107.                   #    frame[int(y), int(x)] = [0,255,0]
  108.                   #for y in range(old_ry, ry, 1 if old_ry < ry else -1):
  109.                   #    x = old_x + (dx * (y - old_ry)) / dry
  110.                   #    frame[int(y), int(x)] = [0,0,255]
  111.                      
  112.               old_x = now_x
  113.               old_by = by
  114.               old_gy = gy
  115.               old_ry = ry
  116.               now_x=now_x+2
  117.          
  118.           cv2.imshow('frame', frame)
  119.         if cv2.waitKey(1) & 0xFF == 27:
  120.             break
  121.    
  122.     cap.release()
  123.     cv2.destroyAllWindows()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top