-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv.py
95 lines (78 loc) · 3.13 KB
/
opencv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import cv2
import matplotlib.pyplot as plt
import random
import requests
random.seed()
def linearSteps(start, end, length=100):
size = np.zeros(length, np.float32)
return cv2.resize(np.array([list(start),list(end)], np.float32), (2,length), size, 0, 0, interpolation=cv2.INTER_LINEAR)
def linearMove(cur, path, frameNum, trans, length=100):
if frameNum % length == 0:
cur += 1
trans = linearSteps(path[cur-1], path[cur], length)
print(trans)
print(path)
if cur == len(path):
return (-1, [], np.float32([[1,0,path[len(path)-1][0]],[0,1,path[len(path)-1][1]]]))
return (cur, trans, np.float32([[1,0,trans[frameNum%length][0]],[0,1,trans[frameNum%length][1]]]))
def followPath(frameNum, img, path, cur, trans, rot):
rows, cols = img.shape[:2]
cur, trans, t1 = linearMove(cur, path, frameNum, trans, 20)
r1 = cv2.getRotationMatrix2D((cols/2,rows/2),rot,1)
rot += 5
rot %= 360
dst1 = cv2.warpAffine(img,t1,(cols,rows))
dst2 = cv2.warpAffine(dst1,r1,(cols,rows))
dst = cv2.addWeighted(dst1, 0.5, dst2, 0.5, 0.0)
return (dst, frameNum, cur, trans, rot)
face_cascade = cv2.CascadeClassifier('../opencv/data/haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('../opencv/data/haarcascades/haarcascade_eye.xml')
cap = cv2.VideoCapture('test3.mp4')
rot = 0
trans = []
ranpath = [(0,0)]
ret, img = cap.read()
rows, cols = img.shape[:2]
ranpath += [(random.randint(-cols/10, cols/10), random.randint(-rows/10, rows/10)) for x in range(100)]
cur = 0
frameNum = 0
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (cols,rows))
r = requests.get('http://127.0.0.1:5000/api/v1/1000000')
payload = eval(r.text)['payload']
print(payload)
fizzImgPre = cv2.imread('fizz.png',-1)
fizzImg = cv2.resize(fizzImgPre,(cols, rows), interpolation = cv2.INTER_CUBIC)
buzzImgPre = cv2.imread('buzz.png',-1)
buzzImg = cv2.resize(buzzImgPre,(cols, rows), interpolation = cv2.INTER_CUBIC)
fizzBuzzImgPre = cv2.imread('fizzbuzz.png',-1)
fizzBuzzImg = cv2.resize(fizzBuzzImgPre,(cols, rows), interpolation = cv2.INTER_CUBIC)
while(cap.isOpened()):
#dst, frameNum, cur, trans, rot = followPath(frameNum, img, ranpath, cur, trans, rot)
dst = img
gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
dst = cv2.rectangle(dst,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = dst[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
#cv2.imshow('test', dst)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if payload[frameNum%len(payload)] == 'Fizz':
out.write(fizzImg)
elif payload[frameNum%len(payload)] == 'Buzz':
out.write(buzzImg)
elif payload[frameNum%len(payload)] == 'FizzBuzz':
out.write(fizzBuzzImg)
else:
out.write(dst)
frameNum += 1
ret, img = cap.read()
cap.release()
out.release()
cv2.destroyAllWindows()