0

I have a script of python which is about object detection once a particular class is identified the servo motor should work Iam using object detection using tensorflow and opencv , basically scripts works by opening the picamera and detecting objects

code for python script and servo give below :(link to entire code https://github.com/aswinr22/waste-model/blob/master/picamera2.py)

(boxes, scores, classes, num) = sess.run(
            [detection_boxes, detection_scores, detection_classes, num_detections],
            feed_dict={image_tensor: frame_expanded})
        # Draw the results of the detection (aka 'visulaize the results')
        vis_util.visualize_boxes_and_labels_on_image_array(
            frame,
            np.squeeze(boxes),
            np.squeeze(classes).astype(np.int32),
            np.squeeze(scores),
            category_index,
            use_normalized_coordinates=True,
            line_thickness=8,
            min_score_thresh=0.40)

        for i in range (classes.size): # here is my classes id is retrieved
            if(classes[0][i] == 2 and scores[0][i]>0.5): # this is my condition , if class id =2 then servo should work

This is the servo code:

import RPi.GPIO as GPIO
import time

servoPIN = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(servoPIN, GPIO.OUT)

p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
p.start(2.5) # Initialization
try:

    p.ChangeDutyCycle(5)
    time.sleep(4)
    p.ChangeDutyCycle(10)
    time.sleep(4)
except KeyboardInterrupt:
  p.stop()
  GPIO.cleanup()

How to combine these both code to work the servo whenever my class id =2 detected

  • It appears that this is actually a learning Python question, nothing really to do with the Raspberry Pi. You probably ought to look through on-line Python tutorials. – joan May 12 at 8:30
  • Hi, please help me to do this , i have searched a lot through online but nothing seems to work iam really confused and frustrated by the way iam a newbie to pi as well as python please help me – rahraj May 12 at 8:53
  • I think the question would be a better fit on raspberrypi.org/forums. – joan May 12 at 8:59
  • With Raspberry Pi you can start with its projects, with Python maybe with Python For Beginners. – Ingo May 12 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.