import cv2
import numpy as np
import random
import os
from PIL import Image
import time
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)
distance_thres = 50
cap = cv2.VideoCapture('data/humans.mp4')
# class_names = [c.strip() for c in open('data/coco.names').readlines()]
def dist(pt1,pt2):
try:
return ((pt1[0]-pt2[0])**2 + (pt1[1]-pt2[1])**2)**0.5
except:
return
layer_names = net.getLayerNames()
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
print('Output layers',output_layers)
_,frame = cap.read()
fourcc = cv2.VideoWriter_fourcc(*"MJPG")
writer = cv2.VideoWriter('output.avi', fourcc, 30,(frame.shape[1], frame.shape[0]), True)
ret = True
while ret:
ret, img = cap.read()
if ret:
height, width = img.shape[:2]
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
confidences = []
boxes = []
for out in outs:
print(out.shape)
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
if class_id!=0:
continue
confidence = scores[class_id]
if confidence > 0.3:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
persons = []
person_centres = []
violate = set()
for i in range(len(boxes)):
if i in indexes:
x,y,w,h = boxes[i]
persons.append(boxes[i])
person_centres.append([x+w//2,y+h//2])
# cv2.rectangle(img, (x, y), (x + w, y + h), (0,255,0), 2)
for i in range(len(persons)):
for j in range(i+1,len(persons)):
if dist(person_centres[i],person_centres[j]) <= distance_thres:
violate.add(tuple(persons[i]))
violate.add(tuple(persons[j]))
v = 0
for (x,y,w,h) in persons:
if (x,y,w,h) in violate:
color = (0,0,255)
v+=1
else:
color = (0,255,0)
cv2.rectangle(img,(x,y),(x+w,y+h),color,2)
cv2.circle(img,(x+w//2,y+h//2),2,(0,0,255),2)
cv2.putText(img,'No of Violations : '+str(v),(15,frame.shape[0]-10),cv2.FONT_HERSHEY_SIMPLEX,1,(0,126,255),2)
writer.write(img)
cv2.imshow("Image", img)
if cv2.waitKey(1) == 27:
break
cap.release()
cv2.destroyAllWindows()
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
python社交隔离源码ai 该项目旨在监测某个地方是否遵循社交距离。 我使用了基于darknet的YOLO架构来构建这个项目。 您必须启用 CUDA 的 OPENCV 才能使用它,否则只需注释掉第 9 行和第 10 行即可。 您还可以在左下角看到一个计数器,指示框架中的违规次数。 人们还可以在网络摄像头上使用它,只需调整第 14 行 --> cap = cv2.VideoCapture(0) 中的一个更改即可。
资源推荐
资源详情
资源评论
收起资源包目录
python社交隔离源码ai.rar (4个子文件)
socialdistancing
data
humans.mp4 1.36MB
coco.names 703B
detect.py 3KB
yolov3.cfg 8KB
共 4 条
- 1
资源评论
小云同志你好
- 粉丝: 1052
- 资源: 1031
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- java-leetcode题解之第111题二叉树的最小深度.zip
- java-leetcode题解之第110题平衡二叉树.zip
- java-leetcode题解之第109题有序链表转换二叉搜索树.zip
- java-leetcode题解之第108题将有序数组转换为二叉搜索树.zip
- java-leetcode题解之第107题二叉树的层序遍历II.zip
- java-leetcode题解之第102题二叉树的层序遍历.zip
- java-leetcode题解之第103题二叉树的锯齿形层序遍历.zip
- java-leetcode题解之第104题二叉树的最大深度.zip
- java-leetcode题解之第173题二叉搜索树迭代器.zip
- java-leetcode题解之第100题相同的树.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功