1
+ import pyautogui
2
+ import cv2
3
+ import mss
4
+ import numpy as np
5
+ import threading
6
+ import time
7
+
8
+ monitor = {'top' : 0 , 'left' : 0 , 'width' : 1366 , 'height' : 768 }
9
+ sct = mss .mss ()
10
+
11
+ color_space_up = np .array ([90 , 90 , 90 ], np .uint8 )
12
+ color_space_dow = np .array ([70 , 70 , 70 ], np .uint8 )
13
+
14
+ color_black = np .array ([0 , 0 , 0 ], np .uint8 )
15
+ color_white = np .array ([255 , 255 , 255 ], np .uint8 )
16
+
17
+ fourcc = cv2 .VideoWriter_fourcc (* 'XVID' )
18
+ out = cv2 .VideoWriter ('output.avi' ,fourcc , 80 , (1366 ,768 ))
19
+
20
+
21
+ def thre_space ():
22
+
23
+ pyautogui .keyDown ('space' )
24
+ time .sleep (0.3 )
25
+ pyautogui .keyUp ('space' )
26
+
27
+
28
+ while True :
29
+
30
+ img = np .array (sct .grab (monitor ))
31
+ frame = cv2 .cvtColor (img , cv2 .COLOR_BGR2RGB )
32
+
33
+ cv2 .rectangle (frame , pt1 = (480 ,190 ), pt2 = (560 ,240 ), color = (0 ,0 ,255 ), thickness = 3 )
34
+ squ0 = frame [190 :240 , 480 :560 , :]
35
+
36
+ cv2 .rectangle (frame , pt1 = (110 ,214 ), pt2 = (176 ,338 ), color = (0 ,0 ,255 ), thickness = 3 )
37
+ squ1 = frame [214 :338 , 110 :176 , :]
38
+
39
+
40
+ mask = cv2 .inRange (squ0 , color_space_dow , color_space_up )
41
+ kernal = np .ones ((5 , 5 ), "uint8" )
42
+ black_mask0 = cv2 .dilate (mask , kernal )
43
+ res_red = cv2 .bitwise_and (squ0 , squ0 , mask = black_mask0 )
44
+ contours , hierarchy = cv2 .findContours (black_mask0 , cv2 .RETR_TREE , cv2 .CHAIN_APPROX_SIMPLE )
45
+
46
+
47
+ for pic , contour in enumerate (contours ):
48
+ area = cv2 .contourArea (contour )
49
+ if (area > 300 ):
50
+ a = threading .Thread (target = thre_space )
51
+ a .start ()
52
+
53
+ mask = cv2 .inRange (squ1 , color_black , color_black )
54
+ kernal = np .ones ((5 , 5 ), "uint8" )
55
+ black_mask0 = cv2 .dilate (mask , kernal )
56
+ res_red = cv2 .bitwise_and (squ1 , squ1 , mask = black_mask0 )
57
+ contours , hierarchy = cv2 .findContours (black_mask0 , cv2 .RETR_TREE , cv2 .CHAIN_APPROX_SIMPLE )
58
+
59
+
60
+ for pic , contour in enumerate (contours ):
61
+ area = cv2 .contourArea (contour )
62
+ if (area > 300 ):
63
+ color_space = np .array ([172 , 172 , 172 ], np .uint8 )
64
+
65
+ color_space_up = np .array ([185 , 185 , 185 ], np .uint8 )
66
+ color_space_dow = np .array ([165 , 165 , 165 ], np .uint8 )
67
+
68
+
69
+ mask = cv2 .inRange (squ1 , color_white , color_white )
70
+ kernal = np .ones ((5 , 5 ), "uint8" )
71
+ black_mask0 = cv2 .dilate (mask , kernal )
72
+ res_red = cv2 .bitwise_and (squ1 , squ1 , mask = black_mask0 )
73
+ contours , hierarchy = cv2 .findContours (black_mask0 , cv2 .RETR_TREE , cv2 .CHAIN_APPROX_SIMPLE )
74
+
75
+
76
+ for pic , contour in enumerate (contours ):
77
+ area = cv2 .contourArea (contour )
78
+ if (area > 300 ):
79
+ color_space_up = np .array ([90 , 90 , 90 ], np .uint8 )
80
+ color_space_dow = np .array ([70 , 70 , 70 ], np .uint8 )
81
+
82
+
83
+
84
+ frame = cv2 .cvtColor (frame , cv2 .COLOR_BGR2RGB )
85
+ out .write (frame )
86
+ if cv2 .waitKey (1 ) & 0xFF == ord ('q' ):
87
+ break
88
+
89
+ out .release ()
0 commit comments