בינה מאלכותית RB14-11 : זיהוי פנים של אדם בתוך תמונה

בינה מאלכותית RB14-11 : זיהוי פנים של אדם בתוך תמונה

 

 

 

 

 

 

Step 1: Load the InsightFace Model

app = FaceAnalysis(name="buffalo_l")
Loads the buffalo_l model (high-quality recognition model).

app.prepare(ctx_id=-1, det_size=(640, 640))
Prepares the model.

  • ctx_id=-1 means run on CPU.

  • det_size=(640, 640) sets the input detection resolution.


Step 2: Cosine Similarity Function

def cosine_similarity(a, b):
Custom function to measure similarity between two face embeddings.
It returns a value between -1 and 1.

  • 1 means identical.

  • 0 means unrelated.

  • Negative means opposite direction (no relation).


Step 3: Load Reference Faces

We store known people’s faces as embeddings.
Each person can have multiple images for better accuracy.

reference_list = [ { "name": "Trump", "paths": […] }, … ]
A list of people with their image paths.

For each image:

  • Open with cv2.imread(path).

  • Detect faces with app.get(img).

  • Extract embedding: faces[0].embedding.

  • Store it in ref["embeddings"].

This means "Trump" will have embeddings from trump1.jpeg, trump2.jpeg, etc.


Step 4: Open Video File

input_video = r"d:\temp\movie_group_photo2.mp4"
Path to input video (or 0 for webcam).

raw_output = r"d:\temp\output_raw.avi"
Temporary AVI file before compression.

final_output = r"d:\temp\output_result.mp4"
Final MP4 file, ready for WhatsApp.

cap = cv2.VideoCapture(input_video)
Opens the video.

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
Codec for AVI (MJPEG is widely supported).

fps = cap.get(cv2.CAP_PROP_FPS)
Gets frames per second from input video.

width, height store video size.

out = cv2.VideoWriter(raw_output, fourcc, fps, (width, height))
Creates video writer object for saving AVI output.


Step 5: Process Video Frame by Frame

while True:
Loop through each frame.

ret, frame = cap.read()
Reads a frame. If ret is False, video ended.

faces = app.get(frame)
Detects all faces in the frame.

For each face:

  • Extract embedding.

  • Compare with all reference embeddings using cosine_similarity.

  • Keep the best match.

  • If similarity < threshold (0.4), label as "Unknown".

cv2.rectangle(frame, (x1, y1), (x2, y2), color, 2)
Draws a box around the detected face.

  • Green if recognized.

  • Red if unknown.

cv2.putText(frame, label, (x1, y1 – 10), …)
Writes the person’s name and confidence percentage above the box.


Step 6: Add Header

header_text = "RoboTronix.co.il 050-6399-001"
Adds your brand header on top of each frame.


Step 7: Save and Preview

out.write(frame)
Writes the processed frame to AVI file.

cv2.imshow("Video Face Recognition", frame)
Shows live preview.
Press q to quit early.


Step 8: Cleanup

cap.release(), out.release()
Releases video input and output.

cv2.destroyAllWindows()
Closes preview window.

כתיבת תגובה