# SPDX-License-Identifier: MIT # Copyright (C) Intel Corporation # # Appearance-Based Search: OpenVINO Runtime sample. # See README.md for prerequisites and inputs. Run: python3 openvino_sample.py import cv2 import numpy as np import openvino as ov DETECTION_MODEL = "intel/person-detection-retail-0013/FP16/person-detection-retail-0013.xml" ATTRIBUTES_MODEL = "intel/person-attributes-recognition-crossroad-0230/FP16/person-attributes-recognition-crossroad-0230.xml" SCENE_VIDEO = "test_video.mp4" # person-attributes-recognition-crossroad-0230 emits eight binary appearance # attributes (output layer "453"), in this order: ATTRIBUTE_NAMES = [ "is_male", "has_bag", "has_backpack", "has_hat", "has_longsleeves", "has_longpants", "has_longhair", "has_coat_jacket", ] # The appearance being searched for. Set an attribute to True to require it or # False to require its absence; omit attributes you do not care about. QUERY = {"has_longhair": True} CONF_THRESHOLD = 0.6 # person-detection confidence ATTR_THRESHOLD = 0.5 # attribute presence threshold core = ov.Core() # Change device to "GPU" or "NPU" to run on integrated GPU or NPU. det_compiled = core.compile_model(core.read_model(DETECTION_MODEL), "CPU") attr_compiled = core.compile_model(core.read_model(ATTRIBUTES_MODEL), "CPU") det_input = det_compiled.input(0) det_h, det_w = det_input.shape[2], det_input.shape[3] attr_input = attr_compiled.input(0) attr_h, attr_w = attr_input.shape[2], attr_input.shape[3] attr_output = attr_compiled.output("453") def detect_persons(img): h0, w0 = img.shape[:2] blob = cv2.resize(img, (det_w, det_h)) blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32) dets = det_compiled([blob])[det_compiled.output(0)][0][0] persons = [] for d in dets: if float(d[2]) < CONF_THRESHOLD: continue x1 = max(0, int(d[3] * w0)) y1 = max(0, int(d[4] * h0)) x2 = min(w0, int(d[5] * w0)) y2 = min(h0, int(d[6] * h0)) if x2 > x1 and y2 > y1: persons.append((x1, y1, x2, y2)) return persons def get_attributes(img, bbox): x1, y1, x2, y2 = bbox crop = img[y1:y2, x1:x2] blob = cv2.resize(crop, (attr_w, attr_h)) blob = blob.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32) values = attr_compiled([blob])[attr_output].flatten() return {name: float(values[i]) for i, name in enumerate(ATTRIBUTE_NAMES)} def matches_query(attributes): return all((attributes[name] >= ATTR_THRESHOLD) == wanted for name, wanted in QUERY.items()) # Scan the scene and keep the frame containing the most people whose appearance # matches the query, so the saved image best illustrates the search result. cap = cv2.VideoCapture(SCENE_VIDEO) total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) or 900 best = {"count": 0, "frame": None, "boxes": []} for frame_idx in range(0, total, 15): cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx) ok, frame = cap.read() if not ok: break boxes = [bbox for bbox in detect_persons(frame) if matches_query(get_attributes(frame, bbox))] if len(boxes) > best["count"]: best = {"count": len(boxes), "frame": frame.copy(), "boxes": boxes} cap.release() if best["frame"] is None or best["count"] == 0: raise SystemExit("No person matching the appearance query was found") # Draw a green box on every person that matches the searched appearance. query_text = ", ".join(k if v else f"no {k}" for k, v in QUERY.items()) frame = best["frame"] for x1, y1, x2, y2 in best["boxes"]: cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2) cv2.putText(frame, f"MATCH: {query_text}", (x1, max(15, y1 - 8)), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2) print(f"Found {best['count']} person(s) matching [{query_text}]") cv2.imwrite("output_openvino.jpg", frame) print("Saved: output_openvino.jpg")