Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- cap = cv2.VideoCapture(0)
- #BGR
- luc = [
- ([17, 15, 100], [50, 56, 200],'RED'),
- ([86, 31, 4], [220, 88, 50], "B"),
- ([25, 146, 190], [62, 174, 250],"Y"),
- ([103, 86, 65], [145, 133, 128],"GRAY"),
- ([40, 210, 100],[40, 200, 30], "GREEN")
- ]
- while(cap.isOpened):
- _, frame = cap.read()
- for (lower, upper, colour) in luc:
- lower = np.array(lower, dtype = "uint8")
- upper = np.array(upper, dtype = "uint8")
- mask = cv2.inRange(frame, lower, upper)
- kernel = np.ones((5,5),'int')
- dilated = cv2.dilate(mask,kernel)
- res = cv2.bitwise_and(frame,frame, mask=mask)
- ret,thrshed = cv2.threshold(cv2.cvtColor(res,cv2.COLOR_BGR2GRAY),3,255,cv2.THRESH_BINARY)
- contours,hier = cv2.findContours(thrshed,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
- for cnt in contours:
- area = cv2.contourArea(cnt)
- if area > 100:
- print(colour)
- # cv2.imshow("frames", np.hstack([frame, res]))
- cv2.imshow("frame", frame)
- k = cv2.waitKey(5) & 0xFF
- if k == 27:
- break
- cap.release()
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement