-
Notifications
You must be signed in to change notification settings - Fork 0
/
playing_with_video.py
176 lines (131 loc) · 4.71 KB
/
playing_with_video.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from cv2 import data
import pygame
import cv2
import sys
import numpy as np
from time import sleep
fps = 30
def color_masking():
# define a video capture object
vid = cv2.VideoCapture(0)
cv2.namedWindow("Trackbars")
def nothing(x):
pass
f = open("color.txt", "r")
l_h = f.readline()
l_s = f.readline()
l_v = f.readline()
u_h = f.readline()
u_s = f.readline()
u_v = f.readline()
f.close()
cv2.createTrackbar("L-H", "Trackbars", int(l_h), 179, nothing)
cv2.createTrackbar("L-s", "Trackbars", int(l_s), 255, nothing)
cv2.createTrackbar("L-v", "Trackbars", int(l_v), 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", int(u_h), 179, nothing)
cv2.createTrackbar("U-S", "Trackbars", int(u_s), 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", int(u_v), 255, nothing)
while(True):
sleep(1 / fps)
# Capture the video frame
# by frame
ret, frame = vid.read()
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
l_h = cv2.getTrackbarPos("L-H", "Trackbars")
l_s = cv2.getTrackbarPos("L-s", "Trackbars")
l_v = cv2.getTrackbarPos("L-v", "Trackbars")
u_h = cv2.getTrackbarPos("U-H", "Trackbars")
u_s = cv2.getTrackbarPos("U-S", "Trackbars")
u_v = cv2.getTrackbarPos("U-V", "Trackbars")
# Color masking
low_HSV = np.array([l_h, l_s, l_v])
high_HSV = np.array([u_h, u_s, u_v])
mask = cv2.inRange(hsv_frame, low_HSV, high_HSV)
#mask = cv2.bitwise_and(frame, frame, mask = mask)
# Display the resulting frame
cv2.imshow('frame', frame)
cv2.imshow("Red", mask)
# if escape is pressed break the loop
if cv2.waitKey(1) == 27:
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
move_on = input("would you like to wirte these values. y/n: ")
if move_on == "y":
f = open("color.txt", "w")
f.write("{}\n{}\n{}\n{}\n{}\n{}".format(str(l_h), str(l_s), str(l_v), str(u_h), str(u_s), str(u_v)))
f.close()
elif move_on == "n":
print("ok then")
def face_detection_all():
x, y, w, h = 0, 0, 0, 0
location_bool = False
# define a video capture object
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
vid = cv2.VideoCapture(0)
while(True):
sleep(1 / fps)
# Capture the video frame
# by frame
ret, frame = vid.read()
#frame = np.fliplr(frame)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
if type(faces) != tuple:
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 0), 3)
print(faces)
else:
print('no face detected')
# Display the resulting frame
cv2.imshow('frame', frame)
if (y > 250 and y != 0) and (x > 250 and x != 0):
location_bool = True
else:
location_bool = False
# if escape is pressed break the loop
if cv2.waitKey(1) == 27:
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
def face_detection_one():
x, y, w, h = 0, 0, 0, 0
location_bool = False
# define a video capture object
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
vid = cv2.VideoCapture(0)
while(True):
sleep(1 / fps)
# Capture the video frame
# by frame
ret, frame = vid.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
if type(faces) != tuple:
one_faces = faces[0]
x = one_faces[0]
y = one_faces[1]
w = one_faces[2]
h = one_faces[3]
cv2.rectangle(frame, (x,y), (x+w, y+h), (255, 0, 0), 3)
print(faces[0])
else:
print('no face detected')
# Display the resulting frame
cv2.imshow('frame', frame)
if (y > 250 and y != 0) and (x > 250 and x != 0):
location_bool = True
else:
location_bool = False
# if escape is pressed break the loop
if cv2.waitKey(1) == 27:
break
# After the loop release the cap object
vid.release()
# Destroy all the windows
cv2.destroyAllWindows()
face_detection_all()