# detect orange -adjustable upper and lower Hue threshholds, lower Saturation and Value threshholds import numpy as np import cv2 def nothing(x): pass at = str(0) cf = cv2.getTickFrequency() font = cv2.FONT_HERSHEY_SIMPLEX # create video capture, trackbars cam = cv2.VideoCapture(0) cv2.namedWindow('Th') cv2.createTrackbar('HL', 'Th',8,180,nothing) cv2.createTrackbar('HH', 'Th',18,180,nothing) cv2.createTrackbar('SL', 'Th',60,255,nothing) cv2.createTrackbar('VL', 'Th',60,255,nothing) # main loop while(1): st = cv2.getTickCount() hl = cv2.getTrackbarPos('HL', 'Th') hh = cv2.getTrackbarPos('HH', 'Th') sl = cv2.getTrackbarPos('SL', 'Th') vl = cv2.getTrackbarPos('VL', 'Th') _, frame = cam.read() frame = cv2.resize(frame, (320, 240), interpolation = cv2.INTER_LINEAR) frame = cv2.blur(frame, (3, 3)) hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) thresh = cv2.inRange(hsv, np.array((hl, sl, vl)), np.array((hh, 255, 255))) thresh2 = thresh.copy() contours, hierarchy = cv2.findContours(thresh, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) max_area = 0 for cnt in contours: area = cv2.contourArea(cnt) if area > max_area: max_area = area best_cnt = cnt if max_area > 0: M = cv2.moments(best_cnt) cx, cy = int(M['m10']/M['m00']), int(M['m01']/M['m00']) cz = int(max_area) cv2.rectangle(frame, (cx - 5, cy - 5), (cx + 5, cy + 5), (0, 0, 255), 2) cv2.putText(frame, at, (0, 50), font, 1, (0, 0, 255), 2) # Show it, if key pressed is 'Esc', exit the loop cv2.imshow('Fr', frame) cv2.imshow('Th', thresh2) et = cv2.getTickCount() at = str('%.1f' % (cf / (et - st))) if cv2.waitKey(1) == 27: break # Clean up everything before leaving cam.release() cv2.destroyAllWindows()