subikshaa / social-distance-detection-using-opencv Goto Github PK
View Code? Open in Web Editor NEWLicense: MIT License
License: MIT License
cv2.rectangle(frame, (startX, startY), (endX, endY), COLOR, 2)
TypeError: only size-1 arrays can be converted to Python scalars
this error came while giving video
distance = (165 * F)/height
Is 165 height of the person?
If a Person is Sitting It says the Person is far and if he too close that camera doesn't capture his whole body then it says two close
My Suggestion is to use the width of the person as it varies by only 2 - 3 cm, and how ever the camera may capture the person ( sitting, standing too close) the width will not affect the Accuracy.
Hi, I'm doing a research about estimating distance between people in video.
I wonder how you can calculate F and set it in source code, and where you get the average height of people ?
Thank you.
cv2.rectangle(frame, (startX, startY), (endX, endY), COLOR, 2)
TypeError: only length-1 arrays can be converted to Python scalars
I am getting this issue running your code and can't understand why
usage: social_distance_detection.py [-h] [-v VIDEO] -m MODEL -p PROTOTXT -l
LABELS [-c CONFIDENCE]
social_distance_detection.py: error: the following arguments are required: -m/--model, -p/--prototxt, -l/--labels
An exception has occurred, use %tb to see the full traceback.
Hey, I was reviewing your code for social distancing. But I am unable to reproduce he o/p.Can you please go through it. I have used TensorFlow API in the to detect.
Attaching the o/p what I am getting.
###############################################################################################
import numpy as np
import tensorflow as tf
import sys
from PIL import Image
import cv2
from collections import defaultdict
from utils import label_map_util
from utils import visualization_utils as vis_util
import math
from itertools import compress, permutations
from PIL import Image, ImageDraw
from scipy.spatial import distance as dist
mask_label_map = label_map_util.load_labelmap('F:/obj_detection/mask_detection/labelmap.pbtxt')
mask_categories = label_map_util.convert_label_map_to_categories(
mask_label_map, max_num_classes=3, use_display_name=True)
mask_category_index = label_map_util.create_category_index(mask_categories)
mask_detection_graph = tf.Graph()
with mask_detection_graph.as_default():
mask_od_graph_def = tf.GraphDef()
with tf.gfile.GFile('F:/obj_detection/mask_detection/frozen_inference_graph.pb', 'rb') as fid:
mask_serialized_graph = fid.read()
mask_od_graph_def.ParseFromString(mask_serialized_graph)
tf.import_graph_def(mask_od_graph_def, name='')
mask_session = tf.Session(graph=mask_detection_graph)
mask_image_tensor = 'image_tensor:0'
mask_detection_boxes = 'detection_boxes:0'
mask_detection_scores = 'detection_scores:0'
mask_detection_classes = 'detection_classes:0'
mask_num_detections = 'num_detections:0'
#category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
general_label_map = label_map_util.load_labelmap('F:/obj_detection/mask_detection/labelmap_person.pbtxt')
general_categories = label_map_util.convert_label_map_to_categories(
general_label_map, max_num_classes=1, use_display_name=True)
general_category_index = label_map_util.create_category_index(general_categories)
general_detection_graph = tf.Graph()
with general_detection_graph.as_default():
general_od_graph_def = tf.GraphDef()
with tf.gfile.GFile('F:/obj_detection/mask_detection/frozen_inference_graph_ssd_lite.pb', 'rb') as fid:
general_serialized_graph = fid.read()
general_od_graph_def.ParseFromString(general_serialized_graph)
tf.import_graph_def(general_od_graph_def, name='')
general_session = tf.Session(graph=general_detection_graph)
general_image_tensor = 'image_tensor:0'
general_detection_boxes = 'detection_boxes:0'
general_detection_scores = 'detection_scores:0'
general_detection_classes = 'detection_classes:0'
general_num_detections = 'num_detections:0'
#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('F:/obj_detection/mask_detection/video.mp4')
ret = True
while (ret):
pos_dict = dict()
coordinates = dict()
ret, image_np = cap.read()
#image_np = cv2.resize(image_np, (640, 360))
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# # Each box represents a part of the image where a particular object was detected.
# boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# # Each score represent how level of confidence for each of the objects.
# # Score is shown on the result image, together with the class label.
# scores = detection_graph.get_tensor_by_name('detection_scores:0')
# classes = detection_graph.get_tensor_by_name('detection_classes:0')
# num_detections = detection_graph.get_tensor_by_name('num_detections:0')
if cap.isOpened():
# get vcap property
width = cap.get(3) # float
height = cap.get(4) # float
# it gives me 0.0 :/
fps = cap.get(7)
#im = Image.fromarray(image_np)
#width, height = im.size
# Actual detection.
(mboxes, mscores, mclasses, mnum) = mask_session.run(
[mask_detection_boxes, mask_detection_scores,
mask_detection_classes, mask_num_detections],
feed_dict={mask_image_tensor: image_np_expanded})
# ----------Predictions
(pred_boxes, pred_scores, pred_classes, pred_num) = general_session.run(
[general_detection_boxes, general_detection_scores,
general_detection_classes, general_num_detections],
feed_dict={general_image_tensor: image_np_expanded})
################ coordinates ######################
coordinates = vis_util.return_coordinates(
image_np,
np.squeeze(pred_boxes),
np.squeeze(pred_scores).astype(np.int32),
np.squeeze(pred_classes),
general_category_index,
use_normalized_coordinates=True,
line_thickness=10,
min_score_thresh=0.85)
#ymin = int(coordinates[0])
#ymax = int(coordinates[1])
#xmin = int(coordinates[2])
#xmax = int(coordinates[3])
for coordinate in coordinates:
(ymin, ymax, xmin, xmax, acc) = coordinate
x_mid = round((xmin + xmax) / 2, 4)
y_mid = round((ymin + ymax) / 2, 4)
height = round(ymax - ymin, 4)
distance = (165 * 600) / height
x_mid_ = (x_mid * distance) / 600
y_mid = (y_mid * distance) / 600
for i in range():
pos_dict[i] = (x_mid, y_mid,distance)
coordinates[i] = (xmin, ymin, xmax, ymax)
close_objects = set()
for i in pos_dict.keys():
for j in pos_dict.keys():
if i < j:
dist = math.sqrt(math.pow(pos_dict[i][0] - pos_dict[j][0], 2) + math.pow(pos_dict[i][1] - pos_dict[j][1], 2) + math.pow(
pos_dict[i][2] - pos_dict[j][2], 2))
# Check if distance less than 2 metres or 200 centimetres
if dist < 200:
close_objects.add(i)
close_objects.add(j)
for i in pos_dict.keys():
if i in close_objects:
COLOR = (0, 0, 255)
else:
COLOR = (0, 255, 0)
(startX, startY, endX, endY) = coordinates[i]
cv2.rectangle(image_np, (startX, startY), (endX, endY), COLOR, 5)
# vis_util.visualize_boxes_and_labels_on_image_array(
# image_np,
# np.squeeze(pred_boxes[i]),
# np.squeeze(pred_classes).astype(np.int32),
# np.squeeze(pred_scores),
# general_category_index,
# use_normalized_coordinates=True,
# line_thickness=8,
# groundtruth_box_visualization_color=COLOR)
y = startY - 15 if startY - 15 > 15 else startY + 15
cv2.putText(image_np, 'Depth: {i} ft'.format(i=round(pos_dict[i][2] / 30.48, 4)), (startX, y),cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLOR, 2)
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(mboxes),
np.squeeze(mclasses).astype(np.int32),
np.squeeze(mscores),
mask_category_index,
use_normalized_coordinates=True,
line_thickness=8)
cv2.imshow('Detected Tools', cv2.resize(image_np, (1000, 850)))
# cv2.imshow('Tools Name',cv2.resize(cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,2),(100,150)))
if cv2.waitKey(25) & 0xFF == ord('q'):
cv2.destroyAllWindows()
cap.release()
break
when i am running the file it gives me this error
TypeError: function takes exactly 4 arguments (2 given)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.