提交 c1953038 authored 作者: blu's avatar blu

evslicer: reset slice time stamps

上级 670be3ac
...@@ -481,12 +481,13 @@ private: ...@@ -481,12 +481,13 @@ private:
//av_dict_set(&pOptsRemux, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0); //av_dict_set(&pOptsRemux, "movflags", "frag_keyframe+empty_moov+default_base_moof", 0);
av_dict_set(&pOptsRemux, "c:v", "libx264", 0); av_dict_set(&pOptsRemux, "c:v", "libx264", 0);
//av_dict_set(&pOptsRemux, "brand", "mp42", 0); //av_dict_set(&pOptsRemux, "brand", "mp42", 0);
//av_dict_set(&pOptsRemux, "movflags", "faststart", 0); av_dict_set(&pOptsRemux, "movflags", "faststart+frag_keyframe", 0);
av_dict_set(&pOptsRemux, "strftime", "1", 0); av_dict_set(&pOptsRemux, "strftime", "1", 0);
av_dict_set(&pOptsRemux, "segment_format", "mp4", 0); av_dict_set(&pOptsRemux, "segment_format", "mp4", 0);
av_dict_set(&pOptsRemux, "f", "segment", 0); av_dict_set(&pOptsRemux, "f", "segment", 0);
av_dict_set(&pOptsRemux, "segment_time", to_string(seconds).data(), 0); av_dict_set(&pOptsRemux, "segment_time", to_string(seconds).data(), 0);
av_dict_set(&pOptsRemux, "segment_wrap", to_string(numSlices).data(), 0); av_dict_set(&pOptsRemux, "segment_wrap", to_string(numSlices).data(), 0);
av_dict_set(&pOptsRemux, "reset_timestamps", "1", 0);
return ret; return ret;
} }
......
...@@ -4,8 +4,21 @@ from cerberus import schema_registry, Validator ...@@ -4,8 +4,21 @@ from cerberus import schema_registry, Validator
from celery import Celery from celery import Celery
from azure.storage.fileshare import ShareFileClient from azure.storage.fileshare import ShareFileClient
import random import random
import os, yaml, logging, time, datetime, threading, json, subprocess, shlex, re, string import requests
import pdb,traceback, sys import os
import yaml
import logging
import time
import datetime
import threading
import json
import subprocess
import shlex
import re
import string
import pdb
import traceback
import sys
random.seed(datetime.datetime.now()) random.seed(datetime.datetime.now())
...@@ -25,8 +38,8 @@ va task schema: ...@@ -25,8 +38,8 @@ va task schema:
VA_SCHEMAS = { VA_SCHEMAS = {
'task': { 'task': {
'cameraId': {'type': 'string', 'dependencies': ['startTime', 'endTime']}, 'cameraId': {'type': 'string', 'dependencies': ['startTime', 'endTime']},
'startTime': {'type': 'integer', 'dependencies':'cameraId'}, 'startTime': {'type': 'integer', 'dependencies': 'cameraId'},
'endTime': {'type': 'integer', 'dependencies':'cameraId'}, 'endTime': {'type': 'integer', 'dependencies': 'cameraId'},
'length': {'type': 'integer'}, 'length': {'type': 'integer'},
'image': {'type': 'string'}, 'image': {'type': 'string'},
'video': {'type': 'string'} 'video': {'type': 'string'}
...@@ -34,38 +47,50 @@ VA_SCHEMAS = { ...@@ -34,38 +47,50 @@ VA_SCHEMAS = {
} }
aan = os.getenv('AAN', 'ilsvideostablediag') aan = os.getenv('AAN', 'ilsvideostablediag')
aak = os.getenv('AAK', 'rWeA/cUiWAsDqGHO0lfDB5eDHNZxCChrH0pMvICdNJR6tt+hE2tHlSl9kUEjqyOY6cztPWaaRbbeoI47uNEeWA==') aak = os.getenv(
CONNSTR="DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix=core.chinacloudapi.cn".format(aan,aak) 'AAK', 'rWeA/cUiWAsDqGHO0lfDB5eDHNZxCChrH0pMvICdNJR6tt+hE2tHlSl9kUEjqyOY6cztPWaaRbbeoI47uNEeWA==')
CONNSTR = "DefaultEndpointsProtocol=https;AccountName={};AccountKey={};EndpointSuffix=core.chinacloudapi.cn".format(
aan, aak)
#CONNSTR1= 'DefaultEndpointsProtocol=https;AccountName=ilsvideostablediag;AccountKey=rWeA/cUiWAsDqGHO0lfDB5eDHNZxCChrH0pMvICdNJR6tt+hE2tHlSl9kUEjqyOY6cztPWaaRbbeoI47uNEeWA==;EndpointSuffix=core.chinacloudapi.cn' #CONNSTR1= 'DefaultEndpointsProtocol=https;AccountName=ilsvideostablediag;AccountKey=rWeA/cUiWAsDqGHO0lfDB5eDHNZxCChrH0pMvICdNJR6tt+hE2tHlSl9kUEjqyOY6cztPWaaRbbeoI47uNEeWA==;EndpointSuffix=core.chinacloudapi.cn'
#if CONNSTR1 != CONNSTR: # if CONNSTR1 != CONNSTR:
# print("======\n\n======= no valid key\n{}\n{}".format(CONNSTR1, CONNSTR)) # print("======\n\n======= no valid key\n{}\n{}".format(CONNSTR1, CONNSTR))
SHARENAME=os.getenv('SHARE','pre-data') SHARENAME = os.getenv('SHARE', 'pre-data')
MQTT_HOST=os.getenv('MQTT_HOST','evcloud.ilabservice.cloud') MQTT_HOST = os.getenv('MQTT_HOST', 'evcloud.ilabservice.cloud')
MQTT_PORT=int(os.getenv('MQTT_PORT', 1883)) MQTT_PORT = int(os.getenv('MQTT_PORT', 1883))
REDIS_ADDR = os.getenv('REDIS', 'redis://localhost:6379') REDIS_ADDR = os.getenv('REDIS', 'redis://localhost:6379')
workd = os.getenv('BIN_DIR', '../') workd = os.getenv('BIN_DIR', '../')
binName = os.getenv('BIN_NAME', 'detector ') binName = os.getenv('BIN_NAME', 'detector ')
binPrefix = os.getenv('BIN_PRE', '') binPrefix = os.getenv('BIN_PRE', '')
configDir = os.getenv('CFG_DIR', workd) configDir = os.getenv('CFG_DIR', workd)
API_HOST = None
API_CAMERA_CFG_URI = "/video/analysis/camera/{}/model"
RABBITMQ_TOPIC = None
CAMERA_CONFIG_MAP = {}
print("CONFIG: \n\tMQTT: {}:{}\n\tBIN_NAME: {}".format(
MQTT_HOST, MQTT_PORT, binName))
print("CONFIG: \n\tMQTT: {}:{}\n\tBIN_NAME: {}".format(MQTT_HOST, MQTT_PORT, binName))
def downloadFile(ipcSn, dirName, fileName, destDir): def downloadFile(ipcSn, dirName, fileName, destDir):
file_path=ipcSn + '/'+dirName+'/'+fileName file_path = ipcSn + '/'+dirName+'/'+fileName
destDir = destDir + '/' + fileName destDir = destDir + '/' + fileName
print("downloading {}: {} {} {}".format(destDir, ipcSn, dirName, file_path)) print("downloading {}: {} {} {}".format(
destDir, ipcSn, dirName, file_path))
with ShareFileClient.from_connection_string(conn_str=CONNSTR, share_name=SHARENAME, file_path=file_path) as fc: with ShareFileClient.from_connection_string(conn_str=CONNSTR, share_name=SHARENAME, file_path=file_path) as fc:
with open(destDir, "wb") as f: with open(destDir, "wb") as f:
data = fc.download_file() data = fc.download_file()
data.readinto(f) data.readinto(f)
def uploadFile(ipcSn, dirName, fileName, srcPath): def uploadFile(ipcSn, dirName, fileName, srcPath):
file_path=ipcSn + '/'+dirName+'/' + fileName file_path = ipcSn + '/'+dirName+'/' + fileName
fc = ShareFileClient.from_connection_string(conn_str=CONNSTR, share_name=SHARENAME, file_path=file_path) fc = ShareFileClient.from_connection_string(
conn_str=CONNSTR, share_name=SHARENAME, file_path=file_path)
with open(srcPath + '/' + fileName, "rb") as source_file: with open(srcPath + '/' + fileName, "rb") as source_file:
fc.upload_file(source_file) fc.upload_file(source_file)
class VAMMQTTClient: class VAMMQTTClient:
# The callback for when the client receives a CONNACK response from the server. # The callback for when the client receives a CONNACK response from the server.
@staticmethod @staticmethod
...@@ -82,7 +107,7 @@ class VAMMQTTClient: ...@@ -82,7 +107,7 @@ class VAMMQTTClient:
@staticmethod @staticmethod
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
payload = msg.payload.decode('utf-8') payload = msg.payload.decode('utf-8')
print(msg.topic+" "+ payload) print(msg.topic+" " + payload)
if userdata: if userdata:
try: try:
jd = json.loads(payload) jd = json.loads(payload)
...@@ -90,24 +115,27 @@ class VAMMQTTClient: ...@@ -90,24 +115,27 @@ class VAMMQTTClient:
except Exception as e: except Exception as e:
print('exception in process message:', e) print('exception in process message:', e)
#extype, value, tb = sys.exc_info() #extype, value, tb = sys.exc_info()
#traceback.print_exc() # traceback.print_exc()
#pdb.post_mortem(tb) # pdb.post_mortem(tb)
@staticmethod @staticmethod
def on_disconnect(client, userdata, rc): def on_disconnect(client, userdata, rc):
#topic = "video.ai/v1.0/task" #topic = "video.ai/v1.0/task"
#client.publish(topic, payload=None, qos=1, retian=False) #client.publish(topic, payload=None, qos=1, retian=False)
print("disconnected") print("disconnected")
def __init__(self, callback, host = MQTT_HOST, port = MQTT_PORT):
def __init__(self, callback, host=MQTT_HOST, port=MQTT_PORT):
''' '''
Parameters Parameters
''' '''
self.client = mqtt.Client("vamqtt",userdata=callback) #, protocol=mqtt.MQTTv5) self.client = mqtt.Client(
"vamqtt", userdata=callback) # , protocol=mqtt.MQTTv5)
self.client.on_connect = VAMMQTTClient.on_connect self.client.on_connect = VAMMQTTClient.on_connect
self.client.on_message = VAMMQTTClient.on_message self.client.on_message = VAMMQTTClient.on_message
self.client.connect_async(host, port, 30) self.client.connect_async(host, port, 30)
self.client.loop_start() self.client.loop_start()
app = Flask(__name__, app = Flask(__name__,
static_url_path='', static_url_path='',
static_folder='web/main/dist') static_folder='web/main/dist')
...@@ -119,13 +147,14 @@ worker = Celery(app.name, broker=app.config['broker_url']) ...@@ -119,13 +147,14 @@ worker = Celery(app.name, broker=app.config['broker_url'])
worker.conf.update(app.config) worker.conf.update(app.config)
worker.conf.update( worker.conf.update(
task_serializer='json', task_serializer='json',
#accept_content=['json'], # accept_content=['json'],
result_serializer='json', result_serializer='json',
#timezone='Europe/Oslo', # timezone='Europe/Oslo',
enable_utc=True) enable_utc=True)
def take_task(task): def take_task(task):
ret = {'code': 0,'msg': 'ok'} ret = {'code': 0, 'msg': 'ok'}
print("taking task", json.dumps(task)) print("taking task", json.dumps(task))
taskValidator = Validator(VA_SCHEMAS['task'], allow_unknown=True) taskValidator = Validator(VA_SCHEMAS['task'], allow_unknown=True)
if not taskValidator.validate(task): if not taskValidator.validate(task):
...@@ -138,10 +167,12 @@ def take_task(task): ...@@ -138,10 +167,12 @@ def take_task(task):
print(json.dumps(ret)) print(json.dumps(ret))
return ret return ret
@app.route('/api/video.ai/v1.0/task', methods=['POST']) @app.route('/api/video.ai/v1.0/task', methods=['POST'])
def new_task(): def new_task():
ret = take_task(request.json) ret = take_task(request.json)
return jsonify(ret); return jsonify(ret)
@worker.task(acks_late=True) @worker.task(acks_late=True)
def video_analysis(data): def video_analysis(data):
...@@ -153,17 +184,28 @@ def video_analysis(data): ...@@ -153,17 +184,28 @@ def video_analysis(data):
if 'cameraId' in data: # azure storage if 'cameraId' in data: # azure storage
# get azure storage video # get azure storage video
ipcSN = data["cameraId"] ipcSN = data["cameraId"]
dirName = "{}-{}".format(data["startTime"],data["endTime"]) dirName = "{}-{}".format(data["startTime"], data["endTime"])
fileName = dirName + '.mp4' fileName = dirName + '.mp4'
strRand = "{}-{}".format(data["startTime"], ''.join(random.choice(string.ascii_letters) for i in range(6))) strRand = "{}-{}".format(data["startTime"], ''.join(
downloadDir = "{}/{}/".format(os.getenv('DL_DIR', workd) + '/' + ipcSN, strRand) random.choice(string.ascii_letters) for i in range(6)))
downloadDir = "{}/{}/".format(os.getenv('DL_DIR',
workd) + '/' + ipcSN, strRand)
os.system('mkdir -p ' + downloadDir) os.system('mkdir -p ' + downloadDir)
downloadFile(ipcSN, dirName, fileName, downloadDir) downloadFile(ipcSN, dirName, fileName, downloadDir)
res = requests.get(API_HOST + API_CAMERA_CFG_URI)
try:
if res.status_code == 200:
pass
except Exception as e:
print("failed to get camera config {}: {}".format(ipcSN, e))
print("downloaded file {} into {}".format(fileName, downloadDir)) print("downloaded file {} into {}".format(fileName, downloadDir))
# analyze # analyze
#cmdLine = '/Users/blu/work/opencv-projects/opencv-yolo/detector /Users/blu/work/opencv-projects/opencv-yolo/web/1550143347000-1577267418999.mp4 -c /Users/blu/work/opencv-projects/opencv-yolo/' #cmdLine = '/Users/blu/work/opencv-projects/opencv-yolo/detector /Users/blu/work/opencv-projects/opencv-yolo/web/1550143347000-1577267418999.mp4 -c /Users/blu/work/opencv-projects/opencv-yolo/'
prefix = binPrefix + ' ' if binPrefix else binPrefix prefix = binPrefix + ' ' if binPrefix else binPrefix
cmdLine = prefix + workd + '/' + binName + ' ' + downloadDir + fileName + ' -c ' + configDir + ' -o ' + downloadDir + '/detect.jpg' cmdLine = prefix + workd + '/' + binName + ' ' + downloadDir + \
fileName + ' -c ' + configDir + ' -o ' + downloadDir + '/detect.jpg'
cmdArgs = shlex.split(cmdLine) cmdArgs = shlex.split(cmdLine)
print(cmdLine, '\n\n', cmdArgs) print(cmdLine, '\n\n', cmdArgs)
output = subprocess.check_output(cmdArgs) output = subprocess.check_output(cmdArgs)
...@@ -174,14 +216,17 @@ def video_analysis(data): ...@@ -174,14 +216,17 @@ def video_analysis(data):
# parse # parse
for line in output.decode('utf-8').split('\n'): for line in output.decode('utf-8').split('\n'):
print("\n", line) print("\n", line)
m = re.match(r".*? found (\w+) ([\d\.]+) .*? image: .*?/([_\w\d]+.jpg).*?time: ([\d\.]+)", line) m = re.match(
r".*? found (\w+) ([\d\.]+) .*? image: .*?/([_\w\d]+.jpg).*?time: ([\d\.]+)", line)
if m: if m:
ret['data']['humanDetect']['found'] = 1 ret['data']['humanDetect']['found'] = 1
ret['data']['humanDetect']['level'] = m.group(2) ret['data']['humanDetect']['level'] = m.group(2)
imageName = m.group(3) imageName = m.group(3)
ret['data']['humanDetect']['image'] = ipcSN + '/' + dirName + '/' + m.group(3) ret['data']['humanDetect']['image'] = ipcSN + \
'/' + dirName + '/' + m.group(3)
ret['data']['humanDetect']['time'] = int(m.group(4)) ret['data']['humanDetect']['time'] = int(m.group(4))
print('found {}: {}, img: {}, elapse: {}'.format(m.group(1), m.group(2), m.group(3), m.group(4))) print('found {}: {}, img: {}, elapse: {}'.format(
m.group(1), m.group(2), m.group(3), m.group(4)))
break break
else: else:
ret['data']['humanDetect']['found'] = 0 ret['data']['humanDetect']['found'] = 0
...@@ -192,11 +237,11 @@ def video_analysis(data): ...@@ -192,11 +237,11 @@ def video_analysis(data):
ret['code'] = 1 ret['code'] = 1
ret['msg'] = 'no video specified' ret['msg'] = 'no video specified'
except Exception as e: except Exception as e:
print("exception in va worker: {}".format(e)); print("exception in va worker: {}".format(e))
ret['code'] = -1 ret['code'] = -1
ret['msg'] = str(e) ret['msg'] = str(e)
#extype, value, tb = sys.exc_info() #extype, value, tb = sys.exc_info()
#traceback.print_exc() # traceback.print_exc()
# write json # write json
jsonFile = downloadDir + '/' + 'result.json' jsonFile = downloadDir + '/' + 'result.json'
with open(jsonFile, 'w') as outfile: with open(jsonFile, 'w') as outfile:
...@@ -217,9 +262,8 @@ def video_analysis(data): ...@@ -217,9 +262,8 @@ def video_analysis(data):
return ret return ret
if __name__ == '__main__': if __name__ == '__main__':
mq = VAMMQTTClient(take_task) mq = VAMMQTTClient(take_task)
app.config['mc'] = mq.client app.config['mc'] = mq.client
app.run(host='0.0.0.0', port = '5000') app.run(host='0.0.0.0', port='5000')
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论