forked from SouravJohar/handy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handy.py
83 lines (64 loc) · 2.69 KB
/
handy.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
import cv2
import math
from Hand import Hand
def detect_face(frame, block=False, colour=(0, 0, 0)):
fill = [1, -1][block]
face_cascade = cv2.CascadeClassifier(
cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
area = 0
X = Y = W = H = 0
for (x, y, w, h) in faces:
if w * h > area:
area = w * h
X, Y, W, H = x, y, w, h
cv2.rectangle(frame, (X, Y), (X + W, Y + H), colour, fill)
def capture_histogram(source=0):
cap = cv2.VideoCapture(source)
while True:
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (1000, 600))
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame, "Place region of the hand inside box and press `A`",
(5, 50), font, 0.7, (255, 255, 255), 2, cv2.LINE_AA)
cv2.rectangle(frame, (500, 100), (580, 180), (105, 105, 105), 2)
box = frame[105:175, 505:575]
cv2.imshow("Capture Histogram", frame)
key = cv2.waitKey(10)
if key == 97:
object_color = box
cv2.destroyAllWindows()
break
if key == 27:
cv2.destroyAllWindows()
cap.release()
break
object_color_hsv = cv2.cvtColor(object_color, cv2.COLOR_BGR2HSV)
object_hist = cv2.calcHist([object_color_hsv], [0, 1], None,
[12, 15], [0, 180, 0, 256])
cv2.normalize(object_hist, object_hist, 0, 255, cv2.NORM_MINMAX)
cap.release()
return object_hist
def locate_object(frame, object_hist):
hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# apply back projection to image using object_hist as
# the model histogram
object_segment = cv2.calcBackProject(
[hsv_frame], [0, 1], object_hist, [0, 180, 0, 256], 1)
disc = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9))
cv2.filter2D(object_segment, -1, disc, object_segment)
_, segment_thresh = cv2.threshold(
object_segment, 70, 255, cv2.THRESH_BINARY)
# apply some image operations to enhance image
kernel = None
eroded = cv2.erode(segment_thresh, kernel, iterations=2)
dilated = cv2.dilate(eroded, kernel, iterations=2)
closing = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernel)
# masking
masked = cv2.bitwise_and(frame, frame, mask=closing)
return closing, masked, segment_thresh
def detect_hand(frame, hist):
detected_hand, masked, raw = locate_object(frame, hist)
return Hand(detected_hand, masked, raw, frame)