Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4971

General discussion • Re: Servo motor

$
0
0
The object detection code is from EdjeElectronics

Code:

while True:    # Start timer (for calculating frame rate)    t1 = cv2.getTickCount()    # Reset trash value count for this frame     # Grab frame from camera    hasFrame, frame1 = cap.read()    # Acquire frame and resize to input shape expected by model [1xHxWx3]    frame = frame1.copy()    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)    frame_resized = cv2.resize(frame_rgb, (width, height))    input_data = np.expand_dims(frame_resized, axis=0)    # Normalize pixel values if using a floating model (i.e. if model is non-quantized)    if floating_model:        input_data = (np.float32(input_data) - input_mean) / input_std    # Perform detection by running the model with the image as input    interpreter.set_tensor(input_details[0]['index'],input_data)    interpreter.invoke()    # Retrieve detection results    boxes = interpreter.get_tensor(output_details[boxes_idx]['index'])[0] # Bounding box coordinates of detected objects    classes = interpreter.get_tensor(output_details[classes_idx]['index'])[0] # Class index of detected objects    scores = interpreter.get_tensor(output_details[scores_idx]['index'])[0] # Confidence of detected objects    # Loop over all detections and process each detection if its confidence is above minimum threshold    for i in range(len(scores)):        if ((scores[i] > min_conf_threshold) and (scores[i] <= 1.0)):            # Get bounding box coordinates            # Interpreter can return coordinates that are outside of image dimensions, need to force them to be within image using max() and min()            ymin = int(max(1,(boxes[i][0] * imH)))            xmin = int(max(1,(boxes[i][1] * imW)))            ymax = int(min(imH,(boxes[i][2] * imH)))            xmax = int(min(imW,(boxes[i][3] * imW)))            # Draw bounding box            cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)            # Get object's name and draw label            object_name = labels[int(classes[i])] # Look up object name from "labels" array using class index            label = '%s: %d%%' % (object_name, int(scores[i]*100)) # Example: 'paper: 72%'            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) # Get font size            label_ymin = max(ymin, labelSize[1] + 10) # Make sure not to draw label too close to top of window            cv2.rectangle(frame, (xmin, label_ymin-labelSize[1]-10), (xmin+labelSize[0], label_ymin+baseLine-10), (255, 255, 255), cv2.FILLED) # Draw white box to put label text in            cv2.putText(frame, label, (xmin, label_ymin-7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) # Draw label text                        if object_name == 'paper':                servo2_pwm.ChangeDutyCycle(5)                time.sleep(2)                servo1_pwm.ChangeDutyCycle(10)                servo1_pwm.ChangeDutyCycle(7.5)                servo2_pwm.ChangeDutyCycle(7.5)            elif object_name == 'plastic':                servo2_pwm.ChangeDutyCycle(10)                time.sleep(2)                servo1_pwm.ChangeDutyCycle(10)                servo1_pwm.ChangeDutyCycle(7.5)                servo2_pwm.ChangeDutyCycle(7.5)            elif object_name == 'metal':                servo2_pwm.ChangeDutyCycle(5)                time.sleep(2)                servo1_pwm.ChangeDutyCycle(10)                servo1_pwm.ChangeDutyCycle(7.5)                servo2_pwm.ChangeDutyCycle(7.5)            elif object_name == 'other_waste':                servo2_pwm.ChangeDutyCycle(10)                time.sleep(2)                servo1_pwm.ChangeDutyCycle(10)                servo1_pwm.ChangeDutyCycle(7.5)                servo2_pwm.ChangeDutyCycle(7.5)                cv2.putText(frame,'FPS: %.2f' % frame_rate_calc,(20,50),cv2.FONT_HERSHEY_PLAIN,1,(0,0,0),4,cv2.LINE_AA)    cv2.putText(frame,'FPS: %.2f' % frame_rate_calc,(20,50),cv2.FONT_HERSHEY_PLAIN,1,(230,230,230),2,cv2.LINE_AA)    # All the results have been drawn on the frame, so it's time to display it.    cv2.imshow('Eco-Bin', frame)    # Calculate framerate    t2 = cv2.getTickCount()    time1 = (t2-t1)/freq    frame_rate_calc= 1/time1    # Press 'q' to quit    if cv2.waitKey(1) == ord('q'):        break# Clean upcv2.destroyAllWindows()cap.release()GPIO.cleanup()

Statistics: Posted by Rynch — Tue Mar 19, 2024 9:17 am



Viewing all articles
Browse latest Browse all 4971

Trending Articles