בינה מאלכותית RB14-11 : זיהוי פנים של אדם בתוך תמונה
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# -*- coding: utf-8 -*- """ Face recognition in a group photo using InsightFace. Supports multiple reference people (Trump, Bill, Mark, etc.) Adds a navy blue header on the result. """ import cv2 import numpy as np from insightface.app import FaceAnalysis # ====================================================== # 1) Load the InsightFace model # ====================================================== app = FaceAnalysis(name="buffalo_l") app.prepare(ctx_id=-1, det_size=(640, 640)) # CPU only def cosine_similarity(a, b): return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b)) # ====================================================== # 2) Load reference faces into an array # ====================================================== reference_list = [ {"name": "Trump", "path": r"d:\temp\trump1.jpeg"}, {"name": "Bill", "path": r"d:\temp\bill1.jpeg"}, {"name": "Mark", "path": r"d:\temp\mark1.jpeg"} ] for ref in reference_list: img = cv2.imread(ref["path"]) if img is None: raise FileNotFoundError(f"Reference image not found: {ref['path']}") faces = app.get(img) if len(faces) == 0: raise ValueError(f"No face detected in reference image: {ref['path']}") ref["embedding"] = faces[0].embedding x1, y1, x2, y2 = map(int, faces[0].bbox) cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(img, f"{ref['name']} (100%)", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2) cv2.imwrite(fr"d:\temp\reference_{ref['name']}.jpg", img) print(f"Saved reference photo with box: d:\\temp\\reference_{ref['name']}.jpg") # ====================================================== # 3) Load group photo # ====================================================== group_img = cv2.imread(r"d:\temp\group_photo1.jpeg") if group_img is None: raise FileNotFoundError("Group photo not found!") faces = app.get(group_img) threshold = 0.4 # ====================================================== # 4) Process faces in group photo # ====================================================== for face in faces: emb = face.embedding x1, y1, x2, y2 = map(int, face.bbox) best_name = "Unknown" best_score = -1 for ref in reference_list: sim = cosine_similarity(ref["embedding"], emb) if sim > best_score: best_score = sim best_name = ref["name"] percent = best_score * 100 if best_score < threshold: best_name = "Unknown" label = f"{best_name} {percent:.1f}%" color = (0, 255, 0) if best_name != "Unknown" else (0, 0, 255) cv2.rectangle(group_img, (x1, y1), (x2, y2), color, 2) cv2.putText(group_img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 2) # ====================================================== # 5) Add small navy blue header # ====================================================== header_text = "RoboTronix.co.il 050-6399-001" navy_blue = (128, 0, 0) # BGR format cv2.putText(group_img, header_text, (20, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, navy_blue, 2) # ====================================================== # 6) Save and show result # ====================================================== cv2.imwrite(r"d:\temp\group_result.jpg", group_img) cv2.imshow("Group Photo Result", group_img) cv2.waitKey(0) cv2.destroyAllWindows() print("Group result saved at: d:\\temp\\group_result.jpg") |
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.