Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加多线程处理 #1

Open
wants to merge 38 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
22e3ece
增加多线程处理
May 27, 2019
6eca323
修改分支readme
May 27, 2019
03369bf
1
May 27, 2019
c6f62ee
打印获取的帧数量
May 28, 2019
8db4744
读取视频录像,处理图片,保存人脸数据
May 28, 2019
d9de21e
修改多线程方式,类结构
May 28, 2019
c64cb83
数据切片保存,客户端通过类似HLS方式分片数据
May 28, 2019
5c5fc60
添加getstream工具,获取指定时间段录像并播放
May 29, 2019
7b3c54a
修改data数据保存格式,便于解析
May 29, 2019
2139559
保存AIServer的处理Demo
May 29, 2019
0ac7c76
保存客户端playDemo
May 29, 2019
0920e38
上传AI处理data数据
May 29, 2019
3cb9ae4
修改ReadMe
May 29, 2019
018a51f
playDmemo允许精确到秒播放
May 30, 2019
d7097be
AIServer保存图片快照
May 30, 2019
2c01fdf
添加GUI界面,显示快照
May 30, 2019
0d618c4
1. Server 增加live命令 不停更新最近的录像 2.更新快照保存策略
May 31, 2019
5409efe
修改线程方式,修复无法退出的问题
May 31, 2019
61f0512
GUI增加点击快照播放视频功能
May 31, 2019
19251f3
playDemo修复bug,从09分播放会失败
May 31, 2019
41fb2d5
修改GUI显示界面
May 31, 2019
1de6bc9
gitignore
May 31, 2019
f4e5890
igonre
May 31, 2019
6dda49b
ignore
May 31, 2019
87ba17d
整理文件
May 31, 2019
f253da0
geStream写入文件用于调试
Jun 4, 2019
1a52df5
修复问题 当一分钟小于900帧时,保存的数据重复
Jun 4, 2019
7ac53fa
修改播放起始时间段
Jun 4, 2019
294a8e2
调试Demo添加帧数据记录
Jun 4, 2019
e0cab42
修改为python3运行
Jun 5, 2019
7eb0627
新增getStream2,使用MT网络检测人脸
Jun 5, 2019
ff1082c
说那个ONet进行检测,提高了检测速度。准确度有所降低
Jun 10, 2019
248d924
修改getStrean 使用MTCNN,参数调节
Jun 10, 2019
dda76a1
隔帧处理,提高处理速度
Jun 11, 2019
403ff1e
AISever与Client使用MTCNN检测
Jun 11, 2019
42953a1
修改服务器地址
Jun 11, 2019
44b6a04
修改readme
Jun 12, 2019
6e025b2
ReadMe
Jun 12, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data/*
snapshoot/*
*.jpg
*.pyc
.vscode/*
184 changes: 184 additions & 0 deletions AIServerDemo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# -*- coding: UTF-8 -*-

import numpy as np
import cv2
import sys
import threading, _thread
from queue import Queue
import time
import json
import MTCNN_Crop_Face

LIVE_URL = "rtsp://admin:admin123@172.16.1.29/cam/realmonitor?channel=1&subtype=0"
RECORD_URL = "rtsp://admin:admin123@172.16.1.16:554/cam/playback?channel=1&subtype=0&starttime=2019_05_27_17_06_00&endtime=2019_05_27_17_08_00"

SEGMENT_TIME = 60 #切片间隔,单位秒
FPS = 15 #帧率,可以从码流中获取
PER_FILE_FRAME = SEGMENT_TIME * FPS

DATA_FILE_PATH = "data/"
SNAP_FILE_PATH = "snapshoot/"

MTC = MTCNN_Crop_Face.MTCNN_Crop_Face()

class DealRecord():

def __init__(self, start_time, end_time):
# threading.Thread.__init__(self, name = "GetPicture")
self.start_time = start_time
self.end_time = end_time
start_timeArray = time.strptime(self.start_time, "%Y_%m_%d_%H_%M_%S")
sart_timeStamp = time.mktime(start_timeArray)
self.time_stamp = sart_timeStamp

self.face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
self.frame_queue = Queue()

if '-' in self.end_time:
record_url = "rtsp://admin:admin123@172.16.1.16:554/cam/playback?channel=1&subtype=0&starttime=" + self.start_time
else:
record_url = "rtsp://admin:admin123@172.16.1.16:554/cam/playback?channel=1&subtype=0&starttime=" + self.start_time + "&endtime=" + self.end_time

self.capture = cv2.VideoCapture(record_url)
print("get %s"%(record_url))

self.stop_flag = False

def run(self):
# 打印视频相关参数,帧率,宽高
if self.capture.isOpened():
print (self.capture.get(cv2.CAP_PROP_FPS))
print (self.capture.get(cv2.CAP_PROP_FRAME_WIDTH))
print (self.capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

_thread.start_new_thread(self.getRecord, ())

self.dealRecord()


def getRecord(self):
while self.capture.isOpened() and not self.stop_flag:
# Capture frame-by-frame
ret, frame = self.capture.read()
# cv2.imshow('image_ori', frame)
if frame is None:
self.frame_queue.put("fileover")
break
else:
# frame = cv2.resize(frame,(864,576))
self.frame_queue.put(frame)
# print("---- put frame.")

print("---- get record finish.")
self.capture.release()


def dealRecord(self):
frame_num = 0
data = []
frame_to_save = None
frame_to_save_num = 0
while 1:
# get接口默认为阻塞接口,会一直等待数据
frame = self.frame_queue.get()

if frame == "fileover":
break

frame_num += 1
# if (frame_num % FPS == 0) or (self.frame_queue.qsize() != 0):
if (frame_num % (FPS*10) == 0):
print("get frame %d. left %d"% (frame_num, self.frame_queue.qsize()))

# 人脸检测
# faces = self.MTC.cropface(frame)
# 隔帧检测
if frame_num % 2 == 1:
faces = MTC.cropface(frame)
last_face = faces
else:
faces = last_face

for bbox in faces:
cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (0, 0, 255), 2)

content = str(faces) + "\n"
if (len(faces) != 0):
# 快照间隔至少10秒
if (frame_num - frame_to_save_num > FPS*10) or (frame_to_save_num == 0):
frame_to_save = frame
frame_to_save_num = frame_num

data.append(content)

# 保存快照
if frame_to_save is not None:
self.savePicture(frame_to_save, frame_to_save_num)
frame_to_save = None

# 写入文件
if (len(data) == PER_FILE_FRAME):
self.saveData(data)
data = []

# cv2.imshow('image',frame)

if cv2.waitKey(1) & 0xFF == ord('q'):
break

self.stop_flag = True
if len(data) > 50:
self.saveData(data)

cv2.destroyAllWindows()
print("---- deal record finish. total frame %d"%(frame_num))


def saveData(self, data):
start_name = time.strftime("%Y%m%d_%H%M%S", time.localtime(self.time_stamp))
file_name = DATA_FILE_PATH + start_name + ".txt"
txt = open(file_name, "w")
for line in data:
txt.write(line)
txt.flush()
txt.close()
print("save to file %s"%(file_name))

self.time_stamp += SEGMENT_TIME


def savePicture(self, frame_to_save, frame_num):
#文件名精确到秒
start_name = time.strftime("%Y%m%d_%H%M", time.localtime(self.time_stamp))
sec = ((frame_num-1) % PER_FILE_FRAME) / FPS
print( "file_num=%d sec=%d"%(frame_num, sec) )
file_name = "%s%s%02d.jpg"%(SNAP_FILE_PATH, start_name, sec)
cv2.imwrite(file_name, frame_to_save)
print("save to file %s"%(file_name))


if __name__ == "__main__":

start_time = "2019_05_27_17_30_00"
end_time = "2019_05_27_17_35_00"

if (len(sys.argv) == 2):
while 1:
timestamp = time.time()
start_time_str = time.strftime("%Y_%m_%d_%H_%M", time.localtime(timestamp - 60)) + "_00"
end_time_str = time.strftime("%Y_%m_%d_%H_%M", time.localtime(timestamp)) + "_00"
print("start=%s end=%s"%(start_time_str, end_time_str))
test = DealRecord(start_time_str, end_time_str)
test.run()

if (len(sys.argv) == 3):
start_time = sys.argv[1]
end_time = sys.argv[2]

test = DealRecord(start_time, end_time)
test.run()

print("exit.")



Loading