object color detection python program youtube

Solutions on MaxInterview for object color detection python program youtube by the best coders in the world

showing results for - "object color detection python program youtube"
Anton
01 Jan 2019
1# Python code for Multiple Color Detection 
2
3import numpy as np 
4import cv2 
5
6
7# Capturing video through webcam 
8webcam = cv2.VideoCapture(0) 
9
10# Start a while loop 
11while(1): 
12	
13	# Reading the video from the 
14	# webcam in image frames 
15	_, imageFrame = webcam.read() 
16
17	# Convert the imageFrame in 
18	# BGR(RGB color space) to 
19	# HSV(hue-saturation-value) 
20	# color space 
21	hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV) 
22
23	# Set range for red color and 
24	# define mask 
25	red_lower = np.array([136, 87, 111], np.uint8) 
26	red_upper = np.array([180, 255, 255], np.uint8) 
27	red_mask = cv2.inRange(hsvFrame, red_lower, red_upper) 
28
29	# Set range for green color and 
30	# define mask 
31	green_lower = np.array([25, 52, 72], np.uint8) 
32	green_upper = np.array([102, 255, 255], np.uint8) 
33	green_mask = cv2.inRange(hsvFrame, green_lower, green_upper) 
34
35	# Set range for blue color and 
36	# define mask 
37	blue_lower = np.array([94, 80, 2], np.uint8) 
38	blue_upper = np.array([120, 255, 255], np.uint8) 
39	blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper) 
40	
41	# Morphological Transform, Dilation 
42	# for each color and bitwise_and operator 
43	# between imageFrame and mask determines 
44	# to detect only that particular color 
45	kernal = np.ones((5, 5), "uint8") 
46	
47	# For red color 
48	red_mask = cv2.dilate(red_mask, kernal) 
49	res_red = cv2.bitwise_and(imageFrame, imageFrame, 
50							mask = red_mask) 
51	
52	# For green color 
53	green_mask = cv2.dilate(green_mask, kernal) 
54	res_green = cv2.bitwise_and(imageFrame, imageFrame, 
55								mask = green_mask) 
56	
57	# For blue color 
58	blue_mask = cv2.dilate(blue_mask, kernal) 
59	res_blue = cv2.bitwise_and(imageFrame, imageFrame, 
60							mask = blue_mask) 
61
62	# Creating contour to track red color 
63	contours, hierarchy = cv2.findContours(red_mask, 
64										cv2.RETR_TREE, 
65										cv2.CHAIN_APPROX_SIMPLE) 
66	
67	for pic, contour in enumerate(contours): 
68		area = cv2.contourArea(contour) 
69		if(area > 300): 
70			x, y, w, h = cv2.boundingRect(contour) 
71			imageFrame = cv2.rectangle(imageFrame, (x, y), 
72									(x + w, y + h), 
73									(0, 0, 255), 2) 
74			
75			cv2.putText(imageFrame, "Red Colour", (x, y), 
76						cv2.FONT_HERSHEY_SIMPLEX, 1.0, 
77						(0, 0, 255))	 
78
79	# Creating contour to track green color 
80	contours, hierarchy = cv2.findContours(green_mask, 
81										cv2.RETR_TREE, 
82										cv2.CHAIN_APPROX_SIMPLE) 
83	
84	for pic, contour in enumerate(contours): 
85		area = cv2.contourArea(contour) 
86		if(area > 300): 
87			x, y, w, h = cv2.boundingRect(contour) 
88			imageFrame = cv2.rectangle(imageFrame, (x, y), 
89									(x + w, y + h), 
90									(0, 255, 0), 2) 
91			
92			cv2.putText(imageFrame, "Green Colour", (x, y), 
93						cv2.FONT_HERSHEY_SIMPLEX, 
94						1.0, (0, 255, 0)) 
95
96	# Creating contour to track blue color 
97	contours, hierarchy = cv2.findContours(blue_mask, 
98										cv2.RETR_TREE, 
99										cv2.CHAIN_APPROX_SIMPLE) 
100	for pic, contour in enumerate(contours): 
101		area = cv2.contourArea(contour) 
102		if(area > 300): 
103			x, y, w, h = cv2.boundingRect(contour) 
104			imageFrame = cv2.rectangle(imageFrame, (x, y), 
105									(x + w, y + h), 
106									(255, 0, 0), 2) 
107			
108			cv2.putText(imageFrame, "Blue Colour", (x, y), 
109						cv2.FONT_HERSHEY_SIMPLEX, 
110						1.0, (255, 0, 0)) 
111			
112	# Program Termination 
113	cv2.imshow("Multiple Color Detection in Real-TIme", imageFrame) 
114	if cv2.waitKey(10) & 0xFF == ord('q'): 
115		cap.release() 
116		cv2.destroyAllWindows() 
117		break
Isabella
17 Jan 2017
1# Python code for Multiple Color Detection
2  
3  
4import numpy as np
5import cv2
6  
7  
8# Capturing video through webcam
9webcam = cv2.VideoCapture(0)
10  
11# Start a while loop
12while(1):
13      
14    # Reading the video from the
15    # webcam in image frames
16    _, imageFrame = webcam.read()
17  
18    # Convert the imageFrame in 
19    # BGR(RGB color space) to 
20    # HSV(hue-saturation-value)
21    # color space
22    hsvFrame = cv2.cvtColor(imageFrame, cv2.COLOR_BGR2HSV)
23  
24    # Set range for red color and 
25    # define mask
26    red_lower = np.array([136, 87, 111], np.uint8)
27    red_upper = np.array([180, 255, 255], np.uint8)
28    red_mask = cv2.inRange(hsvFrame, red_lower, red_upper)
29  
30    # Set range for green color and 
31    # define mask
32    green_lower = np.array([25, 52, 72], np.uint8)
33    green_upper = np.array([102, 255, 255], np.uint8)
34    green_mask = cv2.inRange(hsvFrame, green_lower, green_upper)
35  
36    # Set range for blue color and
37    # define mask
38    blue_lower = np.array([94, 80, 2], np.uint8)
39    blue_upper = np.array([120, 255, 255], np.uint8)
40    blue_mask = cv2.inRange(hsvFrame, blue_lower, blue_upper)
41      
42    # Morphological Transform, Dilation
43    # for each color and bitwise_and operator
44    # between imageFrame and mask determines
45    # to detect only that particular color
46    kernal = np.ones((5, 5), "uint8")
47      
48    # For red color
49    red_mask = cv2.dilate(red_mask, kernal)
50    res_red = cv2.bitwise_and(imageFrame, imageFrame, 
51                              mask = red_mask)
52      
53    # For green color
54    green_mask = cv2.dilate(green_mask, kernal)
55    res_green = cv2.bitwise_and(imageFrame, imageFrame,
56                                mask = green_mask)
57      
58    # For blue color
59    blue_mask = cv2.dilate(blue_mask, kernal)
60    res_blue = cv2.bitwise_and(imageFrame, imageFrame,
61                               mask = blue_mask)
62   
63    # Creating contour to track red color
64    contours, hierarchy = cv2.findContours(red_mask,
65                                           cv2.RETR_TREE,
66                                           cv2.CHAIN_APPROX_SIMPLE)
67      
68    for pic, contour in enumerate(contours):
69        area = cv2.contourArea(contour)
70        if(area > 300):
71            x, y, w, h = cv2.boundingRect(contour)
72            imageFrame = cv2.rectangle(imageFrame, (x, y), 
73                                       (x + w, y + h), 
74                                       (0, 0, 255), 2)
75              
76            cv2.putText(imageFrame, "Red Colour", (x, y),
77                        cv2.FONT_HERSHEY_SIMPLEX, 1.0,
78                        (0, 0, 255))    
79  
80    # Creating contour to track green color
81    contours, hierarchy = cv2.findContours(green_mask,
82                                           cv2.RETR_TREE,
83                                           cv2.CHAIN_APPROX_SIMPLE)
84      
85    for pic, contour in enumerate(contours):
86        area = cv2.contourArea(contour)
87        if(area > 300):
88            x, y, w, h = cv2.boundingRect(contour)
89            imageFrame = cv2.rectangle(imageFrame, (x, y), 
90                                       (x + w, y + h),
91                                       (0, 255, 0), 2)
92              
93            cv2.putText(imageFrame, "Green Colour", (x, y),
94                        cv2.FONT_HERSHEY_SIMPLEX, 
95                        1.0, (0, 255, 0))
96  
97    # Creating contour to track blue color
98    contours, hierarchy = cv2.findContours(blue_mask,
99                                           cv2.RETR_TREE,
100                                           cv2.CHAIN_APPROX_SIMPLE)
101    for pic, contour in enumerate(contours):
102        area = cv2.contourArea(contour)
103        if(area > 300):
104            x, y, w, h = cv2.boundingRect(contour)
105            imageFrame = cv2.rectangle(imageFrame, (x, y),
106                                       (x + w, y + h),
107                                       (255, 0, 0), 2)
108              
109            cv2.putText(imageFrame, "Blue Colour", (x, y),
110                        cv2.FONT_HERSHEY_SIMPLEX,
111                        1.0, (255, 0, 0))
112              
113    # Program Termination
114    cv2.imshow("Multiple Color Detection in Real-TIme", imageFrame)
115    if cv2.waitKey(10) & 0xFF == ord('q'):
116        cap.release()
117        cv2.destroyAllWindows()
118        break