Skip to content

Commit

Permalink
Scripts detect 3 different colors in range (red, green blue)
Browse files Browse the repository at this point in the history
  • Loading branch information
ernestvmo committed May 17, 2022
1 parent 4e5f651 commit 79c87c0
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions ObjectDetection/color_detection_boundbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import numpy as np
import cv2

vcap = cv2.VideoCapture("TestVideos/daniel/20220516-165321.h264")
feed = vcap.isOpened()

while feed:
feed, frame = vcap.read()

# convert frame from BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# set range for red color and define mask
red_lower = np.array([136, 87, 111], np.uint8)
red_upper = np.array([180, 255, 255], np.uint8)
red_mask = cv2.inRange(hsv, red_lower, red_upper)

# set range for green color and define mask
green_lower = np.array([25, 52, 72], np.uint8)
green_upper = np.array([102, 255, 255], np.uint8)
green_mask = cv2.inRange(hsv, green_lower, green_upper)

# set range for blue color and define mask
blue_lower = np.array([94, 80, 2], np.uint8)
blue_upper = np.array([120, 255, 255], np.uint8)
blue_mask = cv2.inRange(hsv, blue_lower, blue_upper)

# Morphological Transform, Dilation
# for each color and bitwise_and operator
# between imageFrame and mask determines
# to detect only that particular color
kernel = np.ones((1, 1), "uint8")

# For red color
red_mask = cv2.dilate(red_mask, kernel)
res_red = cv2.bitwise_and(frame, frame, mask=red_mask)

# For green color
green_mask = cv2.dilate(green_mask, kernel)
res_green = cv2.bitwise_and(frame, frame, mask=green_mask)

# For blue color
blue_mask = cv2.dilate(blue_mask, kernel)
res_blue = cv2.bitwise_and(frame, frame, mask=blue_mask)

# creating contour to track red color
contours, hierarchy = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

for pic, contour in enumerate(contours):
area = cv2.contourArea(contour)
if area > 300:
x, y, w, h = cv2.boundingRect(contour)
frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(frame, "red", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 0, 255))

# # creating contour to track green color
# contours, hierarchy = cv2.findContours(green_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
#
# for pic, contour in enumerate(contours):
# area = cv2.contourArea(contour)
# if area > 300:
# x, y, w, h = cv2.boundingRect(contour)
# frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
#
# cv2.putText(frame, "green", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0))
#
# # creating contour to track blue color
# contours, hierarchy = cv2.findContours(blue_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# for pic, contour in enumerate(contours):
# area = cv2.contourArea(contour)
# if area > 300:
# x, y, w, h = cv2.boundingRect(contour)
# frame = cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)
#
# cv2.putText(frame, "blue", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255, 0, 0))

cv2.imshow("frame", frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
vcap.release()
cv2.destroyAllWindows()
break

0 comments on commit 79c87c0

Please sign in to comment.