|
@@ -5,6 +5,22 @@ import cv2
|
|
|
from stream import StreamCapture
|
|
|
from infer import DoorInference
|
|
|
from logger import logger
|
|
|
+import time
|
|
|
+from datetime import datetime
|
|
|
+
|
|
|
+def is_time_in_period(start_time_str, end_time_str):
|
|
|
+
|
|
|
+ start_time = datetime.strptime(start_time_str, "%H:%M").time()
|
|
|
+ end_time = datetime.strptime(end_time_str, "%H:%M").time()
|
|
|
+ current_time = datetime.now().time()
|
|
|
+
|
|
|
+
|
|
|
+ if start_time <= end_time:
|
|
|
+
|
|
|
+ return start_time <= current_time <= end_time
|
|
|
+ else:
|
|
|
+
|
|
|
+ return current_time >= start_time or current_time <= end_time
|
|
|
|
|
|
async def upload_image(session, url, payload, files):
|
|
|
try:
|
|
@@ -35,57 +51,68 @@ async def process_stream():
|
|
|
channel = '45'
|
|
|
stream = StreamCapture(ip, channel)
|
|
|
|
|
|
+ start_det_time = "06:00"
|
|
|
+ end_det_time = "20:00"
|
|
|
+
|
|
|
posttime = time.time() - 30
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
- for frame, ret in stream():
|
|
|
- if not ret:
|
|
|
- continue
|
|
|
- if time.time() - posttime < 30:
|
|
|
+ while True:
|
|
|
+ if not is_time_in_period(start_det_time, end_det_time):
|
|
|
+ time.sleep(10)
|
|
|
continue
|
|
|
- image = frame.copy()
|
|
|
- result = instance(image)
|
|
|
- if len(result) > 0:
|
|
|
- try:
|
|
|
- posttime = time.time()
|
|
|
- videoTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
|
|
|
- fileTime = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime())
|
|
|
- filename = fileTime + ".jpg"
|
|
|
- filenameori = fileTime + "det.jpg"
|
|
|
- logger.info(videoTime)
|
|
|
- logger.info(result)
|
|
|
-
|
|
|
- for res in result:
|
|
|
- cv2.rectangle(image, tuple(map(int, (res.left, res.top))),
|
|
|
- tuple(map(int, (res.right, res.bottom))), (255, 0, 0), 4)
|
|
|
-
|
|
|
- success, encoded_image = cv2.imencode('.jpg', image)
|
|
|
- if not success:
|
|
|
- logger.error('imencode image error')
|
|
|
- continue
|
|
|
- content = encoded_image.tobytes()
|
|
|
- successori, encoded_imageori = cv2.imencode('.jpg', frame)
|
|
|
- if not successori:
|
|
|
- logger.error('imencode original image error')
|
|
|
- continue
|
|
|
- contentori = encoded_imageori.tobytes()
|
|
|
-
|
|
|
- payload = {
|
|
|
- 'channel': '45',
|
|
|
- 'classIndex': '8',
|
|
|
- 'ip': '172.19.152.231',
|
|
|
- 'videoTime': videoTime,
|
|
|
- 'videoUrl': stream.stream_url
|
|
|
- }
|
|
|
-
|
|
|
- files = [
|
|
|
- ('file', (filename, content, 'image/jpeg')),
|
|
|
- ('oldFile', (filenameori, contentori, 'image/jpeg'))
|
|
|
- ]
|
|
|
-
|
|
|
-
|
|
|
- await upload_image(session, 'http://172.19.152.231/open/api/operate/upload', payload, files)
|
|
|
- except Exception as error:
|
|
|
- logger.error(f'Error: {str(error)}')
|
|
|
+ for frame, ret in stream():
|
|
|
+ if not is_time_in_period(start_det_time, end_det_time):
|
|
|
+ logger.info(f"当前时间不在时间段 {start_det_time} ~ {end_det_time}")
|
|
|
+ stream.close()
|
|
|
+ break
|
|
|
+ if not ret:
|
|
|
+ continue
|
|
|
+ if time.time() - posttime < 30:
|
|
|
+ continue
|
|
|
+ image = frame.copy()
|
|
|
+ result = instance(image)
|
|
|
+ if len(result) > 0:
|
|
|
+ try:
|
|
|
+ posttime = time.time()
|
|
|
+ videoTime = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
|
|
|
+ fileTime = time.strftime('%Y-%m-%d-%H:%M:%S', time.localtime())
|
|
|
+ filename = fileTime + ".jpg"
|
|
|
+ filenameori = fileTime + "det.jpg"
|
|
|
+ logger.info(videoTime)
|
|
|
+ logger.info(result)
|
|
|
+
|
|
|
+ for res in result:
|
|
|
+ cv2.rectangle(image, tuple(map(int, (res.left, res.top))),
|
|
|
+ tuple(map(int, (res.right, res.bottom))), (255, 0, 0), 4)
|
|
|
+
|
|
|
+ success, encoded_image = cv2.imencode('.jpg', image)
|
|
|
+ if not success:
|
|
|
+ logger.error('imencode image error')
|
|
|
+ continue
|
|
|
+ content = encoded_image.tobytes()
|
|
|
+ successori, encoded_imageori = cv2.imencode('.jpg', frame)
|
|
|
+ if not successori:
|
|
|
+ logger.error('imencode original image error')
|
|
|
+ continue
|
|
|
+ contentori = encoded_imageori.tobytes()
|
|
|
+
|
|
|
+ payload = {
|
|
|
+ 'channel': '45',
|
|
|
+ 'classIndex': '8',
|
|
|
+ 'ip': '172.19.152.231',
|
|
|
+ 'videoTime': videoTime,
|
|
|
+ 'videoUrl': stream.stream_url
|
|
|
+ }
|
|
|
+
|
|
|
+ files = [
|
|
|
+ ('file', (filename, content, 'image/jpeg')),
|
|
|
+ ('oldFile', (filenameori, contentori, 'image/jpeg'))
|
|
|
+ ]
|
|
|
+
|
|
|
+
|
|
|
+ await upload_image(session, 'http://172.19.152.231/open/api/operate/upload', payload, files)
|
|
|
+ except Exception as error:
|
|
|
+ logger.error(f'Error: {str(error)}')
|
|
|
|
|
|
logger.info("======= EXIT =======")
|
|
|
|