-2

I am trying to create an robot that can follow a human that I chose, for that I am usig raspberry pi with python and openCV.

I want to create bbox around a human, and I want my camera to track that human, I found pieces of codes on internet and I tried to put them together but when I start the code it gives me image, I can select an object but it doesn't update the frames and the image is freezed.

It also gives me an error when ii press space or another key: "ok = tracker.init(image, bbox) NameError: name 'tracker' is not defined"

Can someone give me some advices? Here is the code only for object tracking:

from picamera import PiCamera
import time
import cv2
import numpy as np

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

while True:
    for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
        image = frame.array
        bbox = cv2.selectROI(image, False)
        ok = tracker.init(image, bbox) 
        cv2.imshow("Camera Output", image)
        #rawCapture.truncate(0)
        ok, bbox = tracker.update(image)
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

        if ok:
            p1 = (int(bbox[0]), int(bbox[1]))
            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
            cv2.rectangle(frame, pi, p2, (255, 0, 0), 2, 1)
        else:
            cv2.putText(image, "Tracking failure detected", (100, 80),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)

        cv2.putText(frame, tracker_type + "Tracker", (100, 20),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
        cv2.putText(image, "FPS:" ++ str(int(fps)), (100, 50),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2);
        cv2.imshow("Tracking", image)

        k = cv2.waitKey(5) #& 0xFF
        if "q" == chr(k & 255):
            break```





Antonio
  • 17
  • 5
  • 1
    Can you post the full trace of the error? – Sarvagya Gupta Mar 15 '20 at 14:18
  • This helps? Select a ROI and then press SPACE or ENTER button! Cancel the selection process by pressing c button! Traceback (most recent call last): File "/home/pi/Desktop/Project/CameraVideo.py", line 17, in ok = tracker.init(image, bbox) NameError: name 'tracker' is not defined – Antonio Mar 15 '20 at 14:21

1 Answers1

0

So the method tracker is not defined. I found this which initializes the method. YOu can do it like this:

tracker = cv2.TrackerKCF_create()

This is considering that you want to implement opencv function

Sarvagya Gupta
  • 861
  • 3
  • 13
  • 28