|
#https://github.com/wangxiao5791509/Pedestrian-Attribute-Recognition-Paper-List |
|
import cv2 |
|
import numpy as np |
|
import sys |
|
import time |
|
import os |
|
|
|
FACE_DATA_DIR = '/home/upsquared/Documents/projects/Code/faces' |
|
DATA_DIR = '/home/upsquared/Documents/projects/Code/samples' |
|
RESULTS_DIR = '/home/upsquared/Documents/projects/Code/results' |
|
ATTRIBUTES_RESULTS_DIR = '/home/upsquared/Documents/projects/Code/attribute_results' |
|
|
|
def Detect_Faces(): |
|
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/face-detection-retail-0004/FP32/face-detection-retail-0004.bin' |
|
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/face-detection-retail-0004/FP32/face-detection-retail-0004.xml' |
|
ped_net = cv2.dnn.readNet(attr_bin, attr_xml) |
|
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) |
|
|
|
print("Models loaded") |
|
files = os.listdir(FACE_DATA_DIR) |
|
#get all files in directory |
|
#loop through for all files |
|
i = 0 |
|
for file in files: |
|
filepath = FACE_DATA_DIR + '//'+ file |
|
print(filepath) |
|
rawframe = cv2.imread(filepath) |
|
frame = cv2.resize(rawframe, (300,300)) |
|
#https://docs.openvinotoolkit.org/latest/_models_intel_person_detection_retail_0013_description_person_detection_retail_0013.html |
|
try: |
|
blob = cv2.dnn.blobFromImage(frame,size=(300,300),ddepth=cv2.CV_8U) |
|
ped_net.setInput(blob) |
|
out = ped_net.forward() |
|
predictions = [] |
|
for detection in out.reshape(-1,7): |
|
image_id,label,conf,x_min,y_min,x_max,y_max = detection |
|
print(conf) |
|
#print(label) |
|
if conf > 0.5: |
|
predictions.append(detection) |
|
#print(predictions) |
|
print(len(predictions)) |
|
for detection in predictions: |
|
confidence = float(detection[2]) |
|
xmin = int(detection[3]*frame.shape[1]) |
|
ymin = int(detection[4]*frame.shape[0]) |
|
xmax = int(detection[5]*frame.shape[1]) |
|
ymax = int(detection[6]*frame.shape[0]) |
|
print(xmin,ymin,xmax,ymax) |
|
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0)) |
|
#Crop and Save |
|
cv2.imshow("Result",frame) |
|
cv2.waitKey(0) |
|
cv2.destroyAllWindows() |
|
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg' |
|
#write the output in directory |
|
cv2.imwrite(result_filepath,frame) |
|
i = i+1 |
|
except: |
|
print('Error') |
|
print(frame) |
|
pass |
|
|
|
def Detect_Pedestrians_Adas(): |
|
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/pedestrian-detection-adas-0002/FP32/pedestrian-detection-adas-0002.bin' |
|
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/pedestrian-detection-adas-0002/FP32/pedestrian-detection-adas-0002.xml' |
|
ped_net = cv2.dnn.readNet(attr_bin, attr_xml) |
|
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) |
|
|
|
print("Models loaded") |
|
files = os.listdir(DATA_DIR) |
|
#get all files in directory |
|
#loop through for all files |
|
i = 0 |
|
for file in files: |
|
filepath = DATA_DIR + '//'+ file |
|
print(filepath) |
|
frame = cv2.imread(filepath) |
|
#https://docs.openvinotoolkit.org/latest/_models_intel_pedestrian_detection_adas_0002_description_pedestrian_detection_adas_0002.html |
|
blob = cv2.dnn.blobFromImage(frame,size=(672,384),ddepth=cv2.CV_8U) |
|
ped_net.setInput(blob) |
|
out = ped_net.forward() |
|
predictions = [] |
|
for detection in out.reshape(-1,7): |
|
image_id,label,conf,x_min,y_min,x_max,y_max = detection |
|
if conf > 0.5: |
|
predictions.append(detection) |
|
#print(predictions) |
|
print(len(predictions)) |
|
for detection in predictions: |
|
confidence = float(detection[2]) |
|
xmin = int(detection[3]*frame.shape[1]) |
|
ymin = int(detection[4]*frame.shape[0]) |
|
xmax = int(detection[5]*frame.shape[1]) |
|
ymax = int(detection[6]*frame.shape[0]) |
|
print(xmin,ymin,xmax,ymax) |
|
pedestrian = frame[ymin:ymax,xmin:xmax] |
|
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg' |
|
#write the output in directory |
|
cv2.imwrite(result_filepath,pedestrian) |
|
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0)) |
|
i = i+1 |
|
cv2.imshow("Result",frame) |
|
cv2.waitKey(0) |
|
cv2.destroyAllWindows() |
|
|
|
def Detect_Pedestrians_Retail(): |
|
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-detection-retail-0013/FP32/person-detection-retail-0013.bin' |
|
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-detection-retail-0013/FP32/person-detection-retail-0013.xml' |
|
ped_net = cv2.dnn.readNet(attr_bin, attr_xml) |
|
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) |
|
|
|
print("Models loaded") |
|
files = os.listdir(DATA_DIR) |
|
#get all files in directory |
|
#loop through for all files |
|
i = 0 |
|
for file in files: |
|
filepath = DATA_DIR + '//'+ file |
|
print(filepath) |
|
frame = cv2.imread(filepath) |
|
#https://docs.openvinotoolkit.org/latest/_models_intel_pedestrian_detection_adas_0002_description_pedestrian_detection_adas_0002.html |
|
#blob = cv2.dnn.blobFromImage(frame,size=(384,672),ddepth=cv2.CV_8U) |
|
#https://docs.openvinotoolkit.org/latest/_models_intel_person_detection_retail_0013_description_person_detection_retail_0013.html |
|
blob = cv2.dnn.blobFromImage(frame,size=(544,320),ddepth=cv2.CV_8U) |
|
ped_net.setInput(blob) |
|
out = ped_net.forward() |
|
predictions = [] |
|
for detection in out.reshape(-1,7): |
|
image_id,label,conf,x_min,y_min,x_max,y_max = detection |
|
#print(conf) |
|
#print(label) |
|
if conf > 0.5: |
|
predictions.append(detection) |
|
#print(predictions) |
|
print(len(predictions)) |
|
for detection in predictions: |
|
confidence = float(detection[2]) |
|
xmin = int(detection[3]*frame.shape[1]) |
|
ymin = int(detection[4]*frame.shape[0]) |
|
xmax = int(detection[5]*frame.shape[1]) |
|
ymax = int(detection[6]*frame.shape[0]) |
|
print(xmin,ymin,xmax,ymax) |
|
pedestrian = frame[ymin:ymax,xmin:xmax] |
|
result_filepath = RESULTS_DIR + '//'+ str(i) + '.jpg' |
|
#write the output in directory |
|
cv2.imwrite(result_filepath,pedestrian) |
|
cv2.rectangle(frame,(xmin,ymin),(xmax,ymax),color=(0,255,0)) |
|
i = i+1 |
|
#Crop and Save |
|
cv2.imshow("Result",frame) |
|
cv2.waitKey(0) |
|
cv2.destroyAllWindows() |
|
|
|
def Detect_Attributes(): |
|
attr_bin = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-attributes-recognition-crossroad-0200/FP32/person-attributes-recognition-crossroad-0200.bin' |
|
attr_xml = '/opt/intel/computer_vision_sdk/deployment_tools/intel_models/person-attributes-recognition-crossroad-0200/FP32/person-attributes-recognition-crossroad-0200.xml' |
|
ped_net = cv2.dnn.readNet(attr_bin, attr_xml) |
|
ped_net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU) |
|
|
|
print("Models loaded") |
|
files = os.listdir(RESULTS_DIR) |
|
#get all files in directory |
|
#loop through for all files |
|
for file in files: |
|
try: |
|
filepath = RESULTS_DIR + '//'+ file |
|
print(filepath) |
|
frame = cv2.imread(filepath,1) |
|
#H = 160 |
|
#W = 80 |
|
h,w = frame.shape[:2] |
|
if (w>=80 and h>=160): |
|
#https://docs.openvinotoolkit.org/latest/_models_intel_person_attributes_recognition_crossroad_0230_description_person_attributes_recognition_crossroad_0230.html |
|
blob = cv2.dnn.blobFromImage(frame,size=(80,160),ddepth=cv2.CV_8U) |
|
ped_net.setInput(blob) |
|
print("part 1") |
|
out = ped_net.forward("453") |
|
#print(out) |
|
predictions = [] |
|
for detection in out.reshape(-1,8): |
|
is_male, has_bag, has_backpack, has_hat, has_longsleeves, has_longpants, has_longhair, has_coat_jacket = detection |
|
predictions.append(detection) |
|
print(predictions) |
|
#print(len(predictions)) |
|
|
|
for detection in predictions: |
|
if(detection[0] > 0.5): |
|
print('MALE') |
|
else: |
|
print('Female') |
|
if(detection[1] > 0.5): |
|
print('Has BAG') |
|
if(detection[2] > 0.5): |
|
print('has_backpack') |
|
if(detection[3] > 0.5): |
|
print('has_hat') |
|
if(detection[4] > 0.5): |
|
print('has_longsleeves') |
|
if(detection[5] > 0.5): |
|
print('has_longpants') |
|
if(detection[6] > 0.5): |
|
print('has_longhair') |
|
if(detection[7] > 0.5): |
|
print('has_coat_jacket') |
|
|
|
print("part 2") |
|
out1 = ped_net.forward("456") |
|
print(out1) |
|
predictions = [] |
|
for detection in out1.reshape(-1,2): |
|
point_with_top_colorx, point_with_top_colory = detection |
|
predictions.append(detection) |
|
for detection in predictions: |
|
print(detection[0]) |
|
print(detection[1]) |
|
|
|
print("part 3") |
|
out2 = ped_net.forward("459") |
|
print(out2) |
|
predictions = [] |
|
for detection in out2.reshape(-1,2): |
|
point_with_bottom_colorx, point_with_bottom_colory = detection |
|
predictions.append(detection) |
|
for detection in predictions: |
|
print(detection[0]) |
|
print(detection[1]) |
|
except: |
|
pass |
|
|
|
#Detect_Faces() |
|
#Detect_Pedestrians_Adas() |
|
#Detect_Pedestrians_Retail() |
|
Detect_Attributes() |
|
|