forked from ayushnawal/Evo-Mouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
101 lines (79 loc) · 3.14 KB
/
test.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
import numpy as np
import cv2
import win32api,win32con
import pyautogui
def click(x,y):
win32api.SetCursorPos((x,y))
#rectangle
def draw_rectangle(event, x, y, flags, params):
global x_init, y_init, drawing, top_left_pt, bottom_right_pt,top,bottom,track_window
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
x_init, y_init = x, y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing:
top_left_pt = (min(x_init, x), min(y_init, y))
bottom_right_pt = (max(x_init, x), max(y_init, y))
img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
top_left_pt = (min(x_init, x), min(y_init, y))
bottom_right_pt = (max(x_init, x), max(y_init, y))
img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x]
top,bottom = top_left_pt,bottom_right_pt
(r,h),(c,w) = top_left_pt,bottom_right_pt
track_window = (r,h,c-r,w-h)
if __name__=='__main__':
drawing= False
top_left_pt, bottom_right_pt=(-1,-1), (-1,-1)
cap = cv2.VideoCapture(0)
cv2.namedWindow('Webcam')
cv2.setMouseCallback('Webcam', draw_rectangle)
while True:
ret, frame =cap.read()
img=cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
(r,h),(c,w) = top_left_pt,bottom_right_pt
img = cv2.rectangle(frame, (r,h), (c,w), 255,2)
cv2.imshow('Webcam',img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
ret,frame = cap.read()
(r,h),(c,w) = top_left_pt,bottom_right_pt
track_window = (r,h,c-r,w-h)
#ROI
roi = frame[r:c,h:w]
hsv_roi = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv_roi, np.array((0., 60.,32.)), np.array((180.,255.,255.)))
roi_hist = cv2.calcHist([hsv_roi],[0],mask,[180],[0,180])
cv2.normalize(roi_hist,roi_hist,0,255,cv2.NORM_MINMAX)
#10 iteration or move by atleast 1 pt
term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 )
top,bottom = top_left_pt,bottom_right_pt
while(1):
ret ,frame = cap.read()
if ret == True:
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
dst = cv2.calcBackProject([hsv],[0],roi_hist,[0,180],1)
# new location
ret, track_window = cv2.meanShift(dst, track_window, term_crit)
# Draw it on image
x,y,w,h = track_window
img2 = cv2.rectangle(frame, (x,y), (x+w,y+h), 255,2)
if (top != top_left_pt) & (bottom != bottom_right_pt):
(r,e),(c,t) = top_left_pt,bottom_right_pt
img = cv2.rectangle(img2, (r,e), (c,t), 255,2)
cv2.imshow('Webcam',img)
pyautogui.moveTo(10,10)
else:
cv2.imshow('Webcam',img2)
k = cv2.waitKey(60) & 0xff
#click(x,y)
if k == 27:
break
else:
cv2.imwrite(chr(k)+".jpg",img2)
else:
break
#click(10,10)
cv2.destroyAllWindows()
cap.release()