Question
I have a color detection code using the Pi Camera and OpenCV. I need help with WHILE and ELSE or IF and ELSE statements in
I have a color detection code using the Pi Camera and OpenCV. I need help with WHILE and ELSE or IF and ELSE statements in the code on recognizing a particular color. I want to know how to write the statement that results in a reaction after the code is detected. Part of the code is below;
#thresh = cv2.inRange(hsv,np.array((0, 200, 200)), np.array((20, 255, 255))) lower = np.array([4,10,120],dtype="uint8") upper = np.array([90,100,255], dtype="uint8") thresh = cv2.inRange(blur, lower, upper) thresh2 = thresh.copy() # find contours in the threshold image image, contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # finding contour with maximum area and store it as best_cnt max_area = 0 best_cnt = 1 for cnt in contours: area = cv2.contourArea(cnt) if area > max_area: max_area = area best_cnt = cnt # finding centroids of best_cnt and draw a circle there M = cv2.moments(best_cnt) cx,cy = int(M['m10']/M['m00']), int(M['m01']/M['m00']) #if best_cnt>1: cv2.circle(blur,(cx,cy),10,(100,100,255),-1) cv2.putText(blur, "Red Detected", (cx - 20, cy - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2) # show the frame cv2.imshow("Frame", blur) #cv2.imshow('thresh',thresh2) key = cv2.waitKey(1) & 0xFF # clear the stream in preparation for the next frame rawCapture.truncate(0) *while (thresh = cv2.inRange(blur, lower, upper)): Thrusters_control.forward() else: Thrusters_control.turn_right()* # if the `q` key was pressed, break from the loop if key == ord("q"): break
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started