Get Informed out of Data

Full width home advertisement

Raspbian

DataScience

Post Page Advertisement [Top]

OpenCV T(Tracking)L(Learning)D(Detection) on Raspberry pi 4

OpenCV T(Tracking)L(Learning)D(Detection) on Raspberry pi 4

 OpenCV - a Computer Vision Library available on python and C++ in most cases. OpenCV is more of a specific extension of scikit library but helps in image transformation.

Actually, OpenCV turns the images into Array or matrices, having a known range of values these matrices can be manipulated into our needs using Linear Algebra.

We have been under serious hype of algorithms, but definitely, it is simple and very much a standard or formula to make an output for N - inputs.

Lets, Make way for Raspberry Pi 4 and OpenCV. OpenCV is a huge library that comes with a variety of options but it also hides the math behind it.


But, TLD - an interesting feature of OpenCV, making it viable for most Edge devices to use AI(a marketing word).

TLD - as the user selects the particular part of a frame in video that can be tracker in all-around video.

Much like the template matching in a video but it gets an update with respect to frames.

The Reason behind a high processing device work better with AI is the conversion speed of a Video to frame and frame to Array differs in devices.

Also, C++ is more runtime based language that's why it has more speed, and python lags at this.

Normal Face Detection based on CNN or DNN would be very much slower on edge devices. But, TLD wins here as it updates the matrices based on the previous one.

It actually works based on P-N (Positive-Negative)or in mechatronics it would be a PD(Proportional -Derivative).

These are methods where data gets manipulated using formula. For example, take an image and convert it into an array.

Now, group certain elements in the array and track them and update it for the following frames based on the previous one.

Here is the python code for running the TLD on raspberry pi.


from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", type=str,
	help="path to input video file")
ap.add_argument("-t", "--tracker", type=str, default="kcf",
	help="OpenCV object tracker type")
args = vars(ap.parse_args())
# initialize a dictionary that maps strings to their corresponding
# OpenCV object tracker implementations
OPENCV_OBJECT_TRACKERS = {
	"tld": cv2.TrackerTLD_create,
}
# initialize OpenCV's special multi-object tracker
trackers = cv2.MultiTracker_create()
# if a video path was not supplied, grab the reference to the web cam
if not args.get("video", False):
	print("[INFO] starting video stream...")
	vs = VideoStream(src=0).start()
	time.sleep(1.0)
# otherwise, grab a reference to the video file
else:
	vs = cv2.VideoCapture(args["video"])
# loop over frames from the video stream
while True:
	# grab the current frame, then handle if we are using a
	# VideoStream or VideoCapture object
	frame = vs.read()
	frame = frame[1] if args.get("video", False) else frame
	# check to see if we have reached the end of the stream
	if frame is None:
		break
	# resize the frame (so we can process it faster)
	frame = imutils.resize(frame, width=600)
# grab the updated bounding box coordinates (if any) for each
	# object that is being tracked
	(success, boxes) = trackers.update(frame)
	# loop over the bounding boxes and draw then on the frame
	for box in boxes:
		(x, y, w, h) = [int(v) for v in box]
		cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
	# show the output frame
	cv2.imshow("Frame", frame)
	key = cv2.waitKey(1) & 0xFF
	# if the 's' key is selected, we are going to "select" a bounding
	# box to track
	if key == ord("s"):
		# select the bounding box of the object we want to track (make
		# sure you press ENTER or SPACE after selecting the ROI)
		box = cv2.selectROI("Frame", frame, fromCenter=False,
			showCrosshair=True)
		# create a new object tracker for the bounding box and add it
		# to our multi-object tracker
		tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
		trackers.add(tracker, frame, box)
	# if the `q` key was pressed, break from the loop
	elif key == ord("q"):
		break
# if we are using a webcam, release the pointer
if not args.get("video", False):
	vs.stop()
# otherwise, release the file pointer
else:
	vs.release()
# close all windows
cv2.destroyAllWindows()
Now, to run this, file with the following command,


 python3 filename --video videofilename or source cam  --tracker tld
I have tested on my raspberry pi 4.

OpenCV comes with several trackers but TLD looks better at tracking. These can also be used for Detection.


No comments:

Post a Comment

Bottom Ad [Post Page]