-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolour_detection.py
More file actions
61 lines (39 loc) · 1.43 KB
/
colour_detection.py
File metadata and controls
61 lines (39 loc) · 1.43 KB
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
import cv2 as cv
import numpy as np
from PIL import Image
webcam = cv.VideoCapture(0)
yellow = [255,0,0]
class Object_Detect():
def loop(self):
while True:
recv, frame = webcam.read()
hsv = cv.cvtColor(frame, cv.COLOR_BGR2HSV)
lower_limit, upper_limit = self.colour_limits(colour=yellow)
mask = cv.inRange(hsv, lower_limit, upper_limit)
maskk=Image.fromarray(mask)
bbox=maskk.getbbox()
yellow_px=np.count_nonzero(mask)
if bbox :
if yellow_px>200:
x1,y1,x2,y2=bbox
frame=cv.rectangle(frame,(x1,y1),(x2,y2),(0,255,0),5)
cv.imshow("fram", frame)
cv.imshow("fram", frame)
key = cv.waitKey(1) & 0xFF
if key == ord('1'):
break
webcam.release()
cv.destroyAllWindows()
def colour_limits(self, colour):
c = np.uint8([[colour]])
hsvc = cv.cvtColor(c, cv.COLOR_BGR2HSV)
lowerlimit = hsvc[0][0][0] - 10, 100, 100
upperlimit = hsvc[0][0][0] + 10, 255,255
lowerlimit = np.array(lowerlimit, dtype=np.uint8)
upperlimit = np.array(upperlimit, dtype=np.uint8)
return lowerlimit, upperlimit
def main(args=None):
detector = Object_Detect()
detector.loop()
if __name__ == '__main__':
main()