Person ReID in real time

There are many ReID codes for OpenCV Python on the internet, but this is apparently the official one: opencv/ at master · opencv/opencv · GitHub

My question is: how do I make it real time? That is, I indicate a query and in real time I traced the person by indicating an ID, and that recognizes who the person at all times (because it currently recognizes the person of the query, but only generates a photo with all the appearances that it has in The galery).

Can you help me?

you need to

  1. understand the code
  2. remove imread from the code and replace with reading from a VideoCapture object

and no, that’s homework, and exhaustively described in the OpenCV tutorials on

if you have a specific issue, ask. if you just don’t feel like reading the documentation and tutorials… that’s unfortunate.

1 Like

I’m not looking to offend anyone, I’m just looking for first-step advice. I did not know that there was a documentation for everything, I am new. I will follow the steps you tell me (including reading the documentation), if I have a problem I’ll let you know.

If you have any more steps you want to suggest to change the code to real time, I will appreciate it.


you can think of the cnn used here as a feature processing tool, instead of comparing images, you compare a much lower dimensional embedding.

the sample there is heavy on gallery processing, maybe it helps, if we extract the bare nessecities:

# once, on startup
net = cv2.dnn.readNet("youtu_reid_baseline_lite.onnx")

def process(net, img):
    blob = cv2.dnn.blobFromImage(img, 0.5, (128,256), (128,128,128), True, False)
    res = net.forward()
    return cv2.normalize(res)

# get features from cropped person images
a = process(net, person_img_a)
b = process(net, person_img_b)

# now you can compare person features using the dot product
dist = np.matmul(a, b)
if dist < some_threshold:
    # same

Honestly, that shape is a bit complicated for me. I am studying HOG in depth. Which do you think is better? If I compare two HOG, that is, that of an image that I have stored of a person with that of a video where that person appears with the same clothes. Do you think it would have the same effect as ReID?

Look at the following code. It’s simple, but, analyze the HOG of the image.

(Histogram of Oriented Gradients explained using OpenCV)

import cv2

import numpy as np

# Python gradient calculation

# Read image

im = cv2.imread('image.jpg')

im = np.float32(im) / 255.0

# Calculate gradient

gx = cv2.Sobel(im, cv2.CV_32F, 1, 0, ksize=1)

gy = cv2.Sobel(im, cv2.CV_32F, 0, 1, ksize=1)

mag, angle = cv2.cartToPolar(gx, gy, angleInDegrees=True)

cv2.imshow("absolute x-gradient",gx)

cv2.imshow("absolute y-gradient",gy)

cv2.imshow("gradient magnitude",mag)

cv2.imshow("gradient direction",angle)


My question is: how do I go about comparing one image with another?

Note: I will appreciate that you share any reading material. I’m learning.

well, you’re at step 2 of 5 there, so, still work to do …
(extract histograms from the mag/angle features)

also, be cautious with satya’s tutorial there, – it’s working with 3 channel images, so you get 3 channel grads, mag, angle images as well (and the sizes/numbers dont quite add up there)

maybe you can also use the builtin HOGDescriptor

extract histograms and compare those (L2, cosine, battacharya, etc)

i doubt, that you 'll get a similar quality for identifying persons

since you’re already at it – try and report your findings ?

1 Like

Simply put, how is ReiD different? I mean, why is it better? Because, if it is better then I will prefer the ReID.

I will do the following with the ReiD:

  1. I will use an object track (the famous one that uses “MobileNetSSD_deploy.caffemodel” I see that it is very good).

  2. I will analyze each person from behind (in another program) that I see with my query. That is, I will run the ReID program automatically only to obtain the values if it is the person who is in the photo of the query or not.

  3. If it is the person who is in the query with the position that gives me “MobileNetSSD_deploy.caffemodel” I will obtain it and that is that I will continue and I will not remove the camera from above.

What do you think?

it’s the SOTA cnn for this task, it was specially trained to re-id humans
(try to read the papers mentioned before )

did you mean “object detection” network ?
(“tracking” is something different !)

1 Like

If an object is detected it can be tracked with the position of the box, am I wrong? But, I am using this code to assign an ID to each person who sees: Simple object tracking with OpenCV - PyImageSearch

That is, my camera will follow the ID that matches the ReID.

naw, sounds like a plan …
come back, if you run into trouble …

1 Like

sorry, i’ve NO IDEA, what that python code does, or why it “misbehaves”.
(i’m a c++ person)

and i’m damn sure, you should stop staring at the sample code, and try to get the idea behind that

1 Like