python - How to print text when camera captures blue color? -


i'm working on script using opencv , python print text when camera captures color. tried use if statement, fails.

here's code:

import cv2 import numpy np  cap = cv2.videocapture(0)  while(1):      # take each frame     _, frame = cap.read()      # convert bgr hsv     hsv = cv2.cvtcolor(frame, cv2.color_bgr2hsv)      # define range of blue color in hsv     lower_blue = np.array([110,50,50])     upper_blue = np.array([130,255,255])     result = lower_blue + upper_blue      # threshold hsv image blue colors     mask = cv2.inrange(hsv, lower_blue, upper_blue)      # bitwise-and mask , original image     res = cv2.bitwise_and(frame,frame, mask= mask)      if result.any() == true:        print 'i can see blue color'      cv2.imshow('frame',frame)     cv2.imshow('mask',mask)     cv2.imshow('res',res)      k = cv2.waitkey(5) & 0xff     if k == 27:         break  cv2.destroyallwindows() 

i worked out solution works environment. i'm using python 2.7 , opencv 2.4.6. may need modify blue_threshold value suit needs.

import cv2 import numpy np  cap = cv2.videocapture(0) blue_threshold = 1000000  # value change works best  while true:      # take each frame     _, frame = cap.read()      # convert bgr hsv     hsv = cv2.cvtcolor(frame, cv2.color_bgr2hsv)      # define range of blue color in hsv     lower_blue = np.array([110,50,50])     upper_blue = np.array([130,255,255])      # threshold hsv image blue colors     mask = cv2.inrange(hsv, lower_blue, upper_blue)     count = mask.sum()      if count > blue_threshold:        print 'i can see blue color'       cv2.imshow('frame',frame)     cv2.imshow('mask',mask)      k = cv2.waitkey(5) & 0xff     if k == 27:         break  cv2.destroyallwindows() 

Comments