用python代码识别货车监控视频中的货车司机是不是东张西望或者抽烟,并记录人物的动作和动作发生的时间导入excel中
时间: 2024-06-11 22:05:33 浏览: 12
这是一个比较复杂的任务,需要使用深度学习和计算机视觉的技术。以下是一个简单的思路:
1. 使用深度学习的目标检测算法,比如YOLOv3或者Faster R-CNN,来检测视频中的货车和司机。
2. 对于司机,可以使用人体姿态估计算法,比如OpenPose,来识别司机的动作,比如东张西望或者抽烟。
3. 对于司机的动作,可以使用OpenCV来计算动作发生的时间,并将结果导入excel中。
以下是一个示例代码,仅供参考:
```python
import cv2
import numpy as np
import pandas as pd
import time
# 加载YOLOv3模型
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
# 加载类别标签
classes = []
with open('coco.names', 'r') as f:
classes = [line.strip() for line in f.readlines()]
# 设置输出层
output_layers = [net.getLayerNames()[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# 加载OpenPose模型
protoFile = "pose_deploy_linevec.prototxt"
weightsFile = "pose_iter_440000.caffemodel"
net = cv2.dnn.readNetFromCaffe(protoFile, weightsFile)
# 设置身体部位的连接顺序
body_parts = ["Nose", "Neck", "RShoulder", "RElbow", "RWrist", "LShoulder", "LElbow", "LWrist", "RHip", "RKnee", "RAnkle", "LHip", "LKnee", "LAnkle", "REye", "LEye", "REar", "LEar"]
pose_pairs = [[1, 2], [1, 5], [2, 3], [3, 4], [5, 6], [6, 7], [1, 8], [8, 9], [9, 10], [1, 11], [11, 12], [12, 13], [1, 0], [0, 14], [14, 16], [0, 15], [15, 17], [2, 17], [5, 16]]
# 打开视频文件
cap = cv2.VideoCapture('truck.mp4')
# 创建空的DataFrame来存储结果
results = pd.DataFrame(columns=['time', 'action'])
while cap.isOpened():
# 读取一帧
ret, frame = cap.read()
if not ret:
break
# 使用YOLOv3模型检测货车和司机
height, width, channels = frame.shape
blob = cv2.dnn.blobFromImage(frame, 1/255, (416, 416), (0,0,0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)
class_ids = []
confidences = []
boxes = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = center_x - w/2
y = center_y - h/2
class_ids.append(class_id)
confidences.append(float(confidence))
boxes.append([x, y, w, h])
indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for i in indices:
i = i[0]
if classes[class_ids[i]] == 'truck':
truck_box = boxes[i]
elif classes[class_ids[i]] == 'person':
person_box = boxes[i]
# 使用OpenPose模型检测司机的动作
x, y, w, h = person_box
person_frame = frame[int(y):int(y+h), int(x):int(x+w)]
blob = cv2.dnn.blobFromImage(person_frame, 1.0/255, (368, 368), (0, 0, 0), swapRB=False, crop=False)
net.setInput(blob)
output = net.forward()
points = []
for i in range(len(body_parts)):
probMap = output[0, i, :, :]
probMap = cv2.resize(probMap, (person_frame.shape[1], person_frame.shape[0]))
minVal, prob, minLoc, point = cv2.minMaxLoc(probMap)
if prob > 0.1:
x = int((person_box[0] + point[0]) * width / person_frame.shape[1])
y = int((person_box[1] + point[1]) * height / person_frame.shape[0])
points.append((x, y))
else:
points.append(None)
for pair in pose_pairs:
partA = pair[0]
partB = pair[1]
if points[partA] and points[partB]:
cv2.line(frame, points[partA], points[partB], (0, 255, 0), 3)
# 判断司机的动作
if points[3] and points[4] and points[5]:
angle1 = np.arctan2(points[5][1]-points[3][1], points[5][0]-points[3][0]) * 180 / np.pi
angle2 = np.arctan2(points[4][1]-points[3][1], points[4][0]-points[3][0]) * 180 / np.pi
if abs(angle1-angle2) > 30:
action = '东张西望'
else:
action = '未知'
elif points[3] and points[6] and points[7]:
angle1 = np.arctan2(points[7][1]-points[3][1], points[7][0]-points[3][0]) * 180 / np.pi
angle2 = np.arctan2(points[6][1]-points[3][1], points[6][0]-points[3][0]) * 180 / np.pi
if abs(angle1-angle2) > 30:
action = '抽烟'
else:
action = '未知'
else:
action = '未知'
# 将结果记录到DataFrame中
results = results.append({'time': time.time(), 'action': action}, ignore_index=True)
# 显示视频和结果
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 保存结果到Excel文件中
results.to_excel('results.xlsx', index=False)
# 清理资源
cap.release()
cv2.destroyAllWindows()
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)