From a54d47c3c11a0fcedbdf51c8d06d73ac18c80985 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 20:15:54 +0800 Subject: [PATCH 01/39] logs --- app.py | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index abdc605..119fe89 100644 --- a/app.py +++ b/app.py @@ -4,10 +4,25 @@ import time import requests import supervision as sv +from datetime import datetime from ultralytics import YOLO from flask import Flask,request,Response from multiprocessing import Process, Event +import torch + +device_count = 0 + +if torch.cuda.is_available(): + device_count = torch.cuda.device_count() + print("GPU is available.") + print("GPU device count:", torch.cuda.device_count()) + print("GPU device Current:", torch.cuda.current_device()) + print("GPU device name:", torch.cuda.get_device_name(torch.cuda.current_device())) +else: + print("GPU is not available.") + + app = Flask(__name__) workers = {} @@ -39,7 +54,15 @@ def stop(id): @app.route('/show',methods=['POST']) def show(): - print(request.data) + # print(request.json) + data = request.json["content"] + balls = data["balls"] + table = data["table"] + text = f"table:{table:>3} " + for i in range(16): + ball = "#" if "ball"+str(i) in balls else " " + text += f"| {i} : {ball} " + print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) return 'ok' def worker(id,source,region=None,stream=False): @@ -60,7 +83,7 @@ def worker(id,source,region=None,stream=False): if not ret: print("Error read video stream.") break - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) + result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%device_count) result = result[0] detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: From c96c2a0b6fd7baaffd8aaca48ea02fea28086e5a Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 20:32:33 +0800 Subject: [PATCH 02/39] torch --- app.py | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index 119fe89..469ea00 100644 --- a/app.py +++ b/app.py @@ -11,18 +11,6 @@ from multiprocessing import Process, Event import torch -device_count = 0 - -if torch.cuda.is_available(): - device_count = torch.cuda.device_count() - print("GPU is available.") - print("GPU device count:", torch.cuda.device_count()) - print("GPU device Current:", torch.cuda.current_device()) - print("GPU device name:", torch.cuda.get_device_name(torch.cuda.current_device())) -else: - print("GPU is not available.") - - app = Flask(__name__) workers = {} @@ -73,7 +61,8 @@ def worker(id,source,region=None,stream=False): model = YOLO('./best.pt') balls = {} count = 0 - print(source) + device_count = torch.cuda.device_count() + print(device_count,source) try: cap = cv2.VideoCapture(source) if not cap.isOpened(): From 951b2f362a669e93eaecb345ec0108ab4a2ea712 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 20:38:58 +0800 Subject: [PATCH 03/39] 4 --- app.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index 469ea00..c6f40b7 100644 --- a/app.py +++ b/app.py @@ -9,8 +9,6 @@ from ultralytics import YOLO from flask import Flask,request,Response from multiprocessing import Process, Event -import torch - app = Flask(__name__) workers = {} @@ -61,8 +59,7 @@ def worker(id,source,region=None,stream=False): model = YOLO('./best.pt') balls = {} count = 0 - device_count = torch.cuda.device_count() - print(device_count,source) + print(source) try: cap = cv2.VideoCapture(source) if not cap.isOpened(): @@ -72,7 +69,7 @@ def worker(id,source,region=None,stream=False): if not ret: print("Error read video stream.") break - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%device_count) + result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: From 95dda6ad25fae08a223dd7acb61fe3e0a5cfed16 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 20:53:27 +0800 Subject: [PATCH 04/39] OPENCV_FFMPEG_READ_ATTEMPTS --- docker-compose.yaml | 1 + show.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 show.py diff --git a/docker-compose.yaml b/docker-compose.yaml index 7d39345..daceb71 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,6 +12,7 @@ services: runtime: nvidia environment: NVIDIA_VISIBLE_DEVICES: all + OPENCV_FFMPEG_READ_ATTEMPTS: 8192 logging: driver: "json-file" options: diff --git a/show.py b/show.py new file mode 100644 index 0000000..28b610b --- /dev/null +++ b/show.py @@ -0,0 +1,28 @@ +import cv2 +import json +import time +import requests +import supervision as sv + +from datetime import datetime +from ultralytics import YOLO +from flask import Flask,request,Response +from multiprocessing import Process, Event + +app = Flask(__name__) + +@app.route('/show',methods=['POST']) +def show(): + # print(request.json) + data = request.json["content"] + balls = data["balls"] + table = data["table"] + text = f"table:{table:>3} " + for i in range(16): + ball = "#" if "ball"+str(i) in balls else " " + text += f"| {i} : {ball} " + print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) + return 'ok' + +if __name__ == '__main__': + app.run("0.0.0.0",threaded=True) From 9379e807d66939333f834c4896ded98622889784 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:02:58 +0800 Subject: [PATCH 05/39] continue --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index c6f40b7..eb3d182 100644 --- a/app.py +++ b/app.py @@ -68,7 +68,7 @@ def worker(id,source,region=None,stream=False): ret, frame = cap.read() if not ret: print("Error read video stream.") - break + continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) From 3abc6000d30c1ecc8ade5657fa2d468b07de12ee Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:07:13 +0800 Subject: [PATCH 06/39] break --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index eb3d182..c6f40b7 100644 --- a/app.py +++ b/app.py @@ -68,7 +68,7 @@ def worker(id,source,region=None,stream=False): ret, frame = cap.read() if not ret: print("Error read video stream.") - continue + break result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) From 12dc9679023989a12e1155c9b45f4144ee5079de Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:18:05 +0800 Subject: [PATCH 07/39] 16384 --- docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index daceb71..9888cec 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,7 +12,7 @@ services: runtime: nvidia environment: NVIDIA_VISIBLE_DEVICES: all - OPENCV_FFMPEG_READ_ATTEMPTS: 8192 + OPENCV_FFMPEG_READ_ATTEMPTS: 16384 logging: driver: "json-file" options: From 1f3a75160012f07c72fae2d120668183df36c0d5 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:20:07 +0800 Subject: [PATCH 08/39] continue --- app.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index c6f40b7..910b397 100644 --- a/app.py +++ b/app.py @@ -68,7 +68,11 @@ def worker(id,source,region=None,stream=False): ret, frame = cap.read() if not ret: print("Error read video stream.") - break + cap = cv2.VideoCapture(source) + if not cap.isOpened(): + print("Error opening video stream.") + break + continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) From 66d6e3a16140fdc7c63c9a447dfc2da70362041a Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:34:31 +0800 Subject: [PATCH 09/39] ..... --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 910b397..d1b8a2d 100644 --- a/app.py +++ b/app.py @@ -63,7 +63,7 @@ def worker(id,source,region=None,stream=False): try: cap = cv2.VideoCapture(source) if not cap.isOpened(): - print("Error opening video stream.") + print("Error opening video stream....") while True: ret, frame = cap.read() if not ret: From 905a21af8f491c8c83d969aa99d3699b6e055f71 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:40:47 +0800 Subject: [PATCH 10/39] .... --- app.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index d1b8a2d..beececc 100644 --- a/app.py +++ b/app.py @@ -63,15 +63,18 @@ def worker(id,source,region=None,stream=False): try: cap = cv2.VideoCapture(source) if not cap.isOpened(): - print("Error opening video stream....") + print("Error opening video stream.") while True: ret, frame = cap.read() if not ret: print("Error read video stream.") cap = cv2.VideoCapture(source) if not cap.isOpened(): - print("Error opening video stream.") - break + print("Error opening video stream..") + cap = cv2.VideoCapture(source) + if not cap.isOpened(): + print("Error opening video stream...") + break continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] From 3940be3370a8e451c4e9e45b11bf03ccf2c555fb Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:47:29 +0800 Subject: [PATCH 11/39] is not --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index beececc..022e7ac 100644 --- a/app.py +++ b/app.py @@ -81,7 +81,7 @@ def worker(id,source,region=None,stream=False): detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) - detections = detections[(detections.tracker_id != None)] + detections = detections[(detections.tracker_id is not None)] count += 1 names = {} From 51f4406c5bcf97180f5b2ff6254120480df3b60c Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:49:23 +0800 Subject: [PATCH 12/39] // --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 022e7ac..716f67f 100644 --- a/app.py +++ b/app.py @@ -79,9 +79,9 @@ def worker(id,source,region=None,stream=False): result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) - if result.boxes.id is not None: - detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) - detections = detections[(detections.tracker_id is not None)] + # if result.boxes.id is not None: + # detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) + # detections = detections[(detections.tracker_id is not None)] count += 1 names = {} From 209807058a6d0bf2b5c898b5d5878fa28dced39f Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:50:38 +0800 Subject: [PATCH 13/39] // --- app.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 716f67f..022e7ac 100644 --- a/app.py +++ b/app.py @@ -79,9 +79,9 @@ def worker(id,source,region=None,stream=False): result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) result = result[0] detections = sv.Detections.from_yolov8(result) - # if result.boxes.id is not None: - # detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) - # detections = detections[(detections.tracker_id is not None)] + if result.boxes.id is not None: + detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) + detections = detections[(detections.tracker_id is not None)] count += 1 names = {} From 7087e87559375fdb2d5dd9d438142672428901e8 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Thu, 21 Sep 2023 21:54:35 +0800 Subject: [PATCH 14/39] != --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index 022e7ac..beececc 100644 --- a/app.py +++ b/app.py @@ -81,7 +81,7 @@ def worker(id,source,region=None,stream=False): detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) - detections = detections[(detections.tracker_id is not None)] + detections = detections[(detections.tracker_id != None)] count += 1 names = {} From b4d2cb204e882f549708442fd7b77ca45d9c9e70 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 11:27:05 +0800 Subject: [PATCH 15/39] app --- app.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index beececc..afadb6e 100644 --- a/app.py +++ b/app.py @@ -72,8 +72,12 @@ def worker(id,source,region=None,stream=False): if not cap.isOpened(): print("Error opening video stream..") cap = cv2.VideoCapture(source) + time.sleep(1) if not cap.isOpened(): print("Error opening video stream...") + time.sleep(3) + if not cap.isOpened(): + print("Error opening video stream....") break continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) @@ -81,7 +85,11 @@ def worker(id,source,region=None,stream=False): detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: detections.tracker_id = result.boxes.id.cpu().numpy().astype(int) - detections = detections[(detections.tracker_id != None)] + else: + detections.tracker_id = np.array([]) + detections.conf = np.array([]) + detections.xyxy=np.empty((0, 4), dtype=np.float32) + # detections = detections[(detections.tracker_id != None)] count += 1 names = {} From e4eda48b02babe41063d440e30027e644ffd96cb Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 11:33:22 +0800 Subject: [PATCH 16/39] import numpy as np --- app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app.py b/app.py index afadb6e..1fd5695 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import cv2 import json import time import requests +import numpy as np import supervision as sv from datetime import datetime From 943b9dfbc7b904862040f465545e00df75c94b25 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 12:01:37 +0800 Subject: [PATCH 17/39] info --- app.py | 13 +++++++++---- docker-compose.yaml | 4 ++-- show.py | 4 +++- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/app.py b/app.py index 1fd5695..bae85d6 100644 --- a/app.py +++ b/app.py @@ -43,6 +43,10 @@ def stop(id): def show(): # print(request.json) data = request.json["content"] + info(data) + return 'ok' + +def info(data): balls = data["balls"] table = data["table"] text = f"table:{table:>3} " @@ -50,7 +54,6 @@ def show(): ball = "#" if "ball"+str(i) in balls else " " text += f"| {i} : {ball} " print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) - return 'ok' def worker(id,source,region=None,stream=False): if region == None: @@ -111,7 +114,9 @@ def worker(id,source,region=None,stream=False): names = {} if count < region: continue - json_data = json.dumps({"table":id,"balls":balls,"time":int(time.time()*1000)}) + data = {"table":id,"balls":balls,"time":int(time.time()*1000)} + info(data) + json_data = json.dumps(data) balls = {} count = 0 if not stream: @@ -135,10 +140,10 @@ def workerloop(stop_event,id,source,target=None,region=None): "Content-Type": "application/json" } text = json.dumps({"content":json_data}) - print(text) + # print(text) response = requests.post(target, data=text, headers=headers) - print(response.text) + # print(response.text) finally: gen.close() diff --git a/docker-compose.yaml b/docker-compose.yaml index 9888cec..4c4c90e 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,8 +1,8 @@ version: '3' services: billiard: - # command: ["python", "./app.py"] - command: ["gunicorn", "app:app", "--bind", "0.0.0.0:5000", "--timeout", "0", "--workers", "1"] + command: ["python", "./app.py"] + # command: ["gunicorn", "app:app", "--bind", "0.0.0.0:5000", "--timeout", "0", "--workers", "1"] restart: always build: . volumes: diff --git a/show.py b/show.py index 28b610b..5a5e758 100644 --- a/show.py +++ b/show.py @@ -1,6 +1,7 @@ import cv2 import json import time +import logging import requests import supervision as sv @@ -23,6 +24,7 @@ def show(): text += f"| {i} : {ball} " print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) return 'ok' - + if __name__ == '__main__': + app.logger.setLevel(logging.ERROR) app.run("0.0.0.0",threaded=True) From 1cdc8f2e94cf8ca2a750558842c367a3788501e0 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 12:26:02 +0800 Subject: [PATCH 18/39] gunicorn --- docker-compose.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 4c4c90e..9888cec 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,8 +1,8 @@ version: '3' services: billiard: - command: ["python", "./app.py"] - # command: ["gunicorn", "app:app", "--bind", "0.0.0.0:5000", "--timeout", "0", "--workers", "1"] + # command: ["python", "./app.py"] + command: ["gunicorn", "app:app", "--bind", "0.0.0.0:5000", "--timeout", "0", "--workers", "1"] restart: always build: . volumes: From bd158261ef5f1e5712bf5a2bf3371842498acbf8 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 12:30:27 +0800 Subject: [PATCH 19/39] del --- app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app.py b/app.py index bae85d6..cce2c75 100644 --- a/app.py +++ b/app.py @@ -85,6 +85,8 @@ def worker(id,source,region=None,stream=False): break continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) + del(ret) + del(frame) result = result[0] detections = sv.Detections.from_yolov8(result) if result.boxes.id is not None: From 666782c109f8247e6d4f133a6824d56c1fb19ac1 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 12:45:52 +0800 Subject: [PATCH 20/39] ret, frame = cap.read() --- app.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app.py b/app.py index cce2c75..28a1491 100644 --- a/app.py +++ b/app.py @@ -69,6 +69,7 @@ def worker(id,source,region=None,stream=False): if not cap.isOpened(): print("Error opening video stream.") while True: + ret, frame = cap.read() ret, frame = cap.read() if not ret: print("Error read video stream.") From acfa96c3ebdaf4dd1e6699bc5d06097dd7365f3f Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 12:49:01 +0800 Subject: [PATCH 21/39] del --- app.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app.py b/app.py index 28a1491..cce2c75 100644 --- a/app.py +++ b/app.py @@ -69,7 +69,6 @@ def worker(id,source,region=None,stream=False): if not cap.isOpened(): print("Error opening video stream.") while True: - ret, frame = cap.read() ret, frame = cap.read() if not ret: print("Error read video stream.") From 22ebe5d2d66a8a144b088a50d20c995e050191ce Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 13:59:08 +0800 Subject: [PATCH 22/39] threading --- app.py | 32 +++++++++++++++++++++++++------- show.py | 2 +- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index cce2c75..a6c9cb6 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import cv2 import json import time import requests +import threading import numpy as np import supervision as sv @@ -69,7 +70,10 @@ def worker(id,source,region=None,stream=False): if not cap.isOpened(): print("Error opening video stream.") while True: + use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + bgn = (time.time()*1000) ret, frame = cap.read() + use += " read:"+str((time.time()*1000) - bgn) if not ret: print("Error read video stream.") cap = cv2.VideoCapture(source) @@ -85,6 +89,7 @@ def worker(id,source,region=None,stream=False): break continue result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) + use += " track:"+str((time.time()*1000) - bgn) del(ret) del(frame) result = result[0] @@ -114,6 +119,7 @@ def worker(id,source,region=None,stream=False): balls[name] = ball names[name] = True names = {} + use += " names:"+str((time.time()*1000) - bgn) if count < region: continue data = {"table":id,"balls":balls,"time":int(time.time()*1000)} @@ -121,10 +127,13 @@ def worker(id,source,region=None,stream=False): json_data = json.dumps(data) balls = {} count = 0 + use += " dump:"+str((time.time()*1000) - bgn) if not stream: yield json_data else: yield f"data: {json_data}\n\n" + use += " yield:"+str((time.time()*1000) - bgn) + print(use) except GeneratorExit: print("Client disconnected at", time.ctime()) @@ -134,21 +143,30 @@ def workerloop(stop_event,id,source,target=None,region=None): try: gen = worker(id,source,region) for data in gen: + use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + bgn = (time.time()*1000) if stop_event.is_set(): break json_data = json.loads(data) - # 设置请求头 - headers = { - "Content-Type": "application/json" - } + use += " loads:"+str((time.time()*1000) - bgn) text = json.dumps({"content":json_data}) - # print(text) - response = requests.post(target, data=text, headers=headers) + use += " dumps:"+str((time.time()*1000) - bgn) - # print(response.text) + # 创建并启动线程 + thread = threading.Thread(target=post_request, args=(target, text)) + thread.start() + + use += " post:"+str((time.time()*1000) - bgn) + print("gen",use) finally: gen.close() +def post_request(url, data): + headers = { + "Content-Type": "application/json" + } + response = requests.post(url, data=data,headers=headers) + print(response.text) if __name__ == '__main__': app.run("0.0.0.0",threaded=True) diff --git a/show.py b/show.py index 5a5e758..320a539 100644 --- a/show.py +++ b/show.py @@ -27,4 +27,4 @@ def show(): if __name__ == '__main__': app.logger.setLevel(logging.ERROR) - app.run("0.0.0.0",threaded=True) + app.run("0.0.0.0",port=8000,threaded=True) From fd8dff877f8ed3b17e3aa9f946197c66cea9f4cd Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 14:08:22 +0800 Subject: [PATCH 23/39] read remove --- app.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app.py b/app.py index a6c9cb6..662e88f 100644 --- a/app.py +++ b/app.py @@ -73,6 +73,9 @@ def worker(id,source,region=None,stream=False): use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') bgn = (time.time()*1000) ret, frame = cap.read() + del(ret) + del(frame) + ret, frame = cap.read() use += " read:"+str((time.time()*1000) - bgn) if not ret: print("Error read video stream.") From 089b4dc651b7ccf82ea1dfdc50e72064d23ee396 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 15:22:23 +0800 Subject: [PATCH 24/39] cProfile --- app.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/app.py b/app.py index 662e88f..19c57d0 100644 --- a/app.py +++ b/app.py @@ -171,5 +171,19 @@ def post_request(url, data): response = requests.post(url, data=data,headers=headers) print(response.text) +# import pstats +# import cProfile +# def test_function(): +# url = "rtmp://rtmp03open.ys7.com:1935/v3/openlive/L05874022_1_1?expire=1726389273&id=625006970167889920&t=2c7f371063e4ad672cb7a7c34b2a236e9ff37b02052f818c9c4fe7ae9632cd0e&ev=100" +# gen = worker(0,url,5) +# count = 0 +# for data in gen: +# count += 1 +# if count > 10: +# break + if __name__ == '__main__': + # cProfile.run('test_function()', 'test_function.profile') + # p = pstats.Stats('test_function.profile') + # p.sort_stats('cumulative').print_stats(100) # Top 10 by cumulative time app.run("0.0.0.0",threaded=True) From 2d40d4251c6ee64c798a5ab349a157a0c4a4b432 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 15:25:34 +0800 Subject: [PATCH 25/39] logs --- app.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 19c57d0..b346522 100644 --- a/app.py +++ b/app.py @@ -61,10 +61,11 @@ def worker(id,source,region=None,stream=False): region = 10 else: region = int(region) + print("start loading model...",id,source,region) model = YOLO('./best.pt') + print("start loaded model!!!") balls = {} count = 0 - print(source) try: cap = cv2.VideoCapture(source) if not cap.isOpened(): From 66851f9b27ef9531fbaed2f80b56037aaf662702 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 15:34:41 +0800 Subject: [PATCH 26/39] OPENCV_FFMPEG_READ_ATTEMPTS --- docker-compose.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index 9888cec..c890073 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,7 +12,7 @@ services: runtime: nvidia environment: NVIDIA_VISIBLE_DEVICES: all - OPENCV_FFMPEG_READ_ATTEMPTS: 16384 + # OPENCV_FFMPEG_READ_ATTEMPTS: 16384 logging: driver: "json-file" options: From 62a80cd000ffe7c9cc21f12670d906f1e29314a5 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 15:55:28 +0800 Subject: [PATCH 27/39] logs --- app.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index b346522..0d486c1 100644 --- a/app.py +++ b/app.py @@ -137,7 +137,7 @@ def worker(id,source,region=None,stream=False): else: yield f"data: {json_data}\n\n" use += " yield:"+str((time.time()*1000) - bgn) - print(use) + print("model.track",use) except GeneratorExit: print("Client disconnected at", time.ctime()) @@ -161,16 +161,20 @@ def workerloop(stop_event,id,source,target=None,region=None): thread.start() use += " post:"+str((time.time()*1000) - bgn) - print("gen",use) + print("workerloop",use) finally: gen.close() def post_request(url, data): + use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + bgn = (time.time()*1000) headers = { "Content-Type": "application/json" } response = requests.post(url, data=data,headers=headers) print(response.text) + use += " post:"+str((time.time()*1000) - bgn + "ms") + print("post_request",use) # import pstats # import cProfile From 3eb207a388fd51706c7408667858906b21f0cfa8 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 19:51:49 +0800 Subject: [PATCH 28/39] response.text --- app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app.py b/app.py index 0d486c1..aaa163b 100644 --- a/app.py +++ b/app.py @@ -172,9 +172,8 @@ def post_request(url, data): "Content-Type": "application/json" } response = requests.post(url, data=data,headers=headers) - print(response.text) use += " post:"+str((time.time()*1000) - bgn + "ms") - print("post_request",use) + print("post_request",use,response.text) # import pstats # import cProfile From 5526ffef7a87491adfbd3bcb117c1e15328a8f66 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 19:58:24 +0800 Subject: [PATCH 29/39] "ms" --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index aaa163b..36f823e 100644 --- a/app.py +++ b/app.py @@ -172,7 +172,7 @@ def post_request(url, data): "Content-Type": "application/json" } response = requests.post(url, data=data,headers=headers) - use += " post:"+str((time.time()*1000) - bgn + "ms") + use += " post:"+str((time.time()*1000) - bgn ) +"ms" print("post_request",use,response.text) # import pstats From 07c5e5814903555d73b203aee6bb6a69af8dd3f8 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 20:07:25 +0800 Subject: [PATCH 30/39] PYTHONUNBUFFERED --- docker-compose.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yaml b/docker-compose.yaml index c890073..068a16a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -12,6 +12,7 @@ services: runtime: nvidia environment: NVIDIA_VISIBLE_DEVICES: all + PYTHONUNBUFFERED: 1 # OPENCV_FFMPEG_READ_ATTEMPTS: 16384 logging: driver: "json-file" From 05bdd0abfeea6230af822a58b10c858d6d93bf4b Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Fri, 22 Sep 2023 20:50:12 +0800 Subject: [PATCH 31/39] device_count --- app.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index 36f823e..c38a584 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,7 @@ import cv2 import json import time +import torch import requests import threading import numpy as np @@ -61,6 +62,8 @@ def worker(id,source,region=None,stream=False): region = 10 else: region = int(region) + device_count = torch.cuda.device_count() + print("GPU device count:", device_count) print("start loading model...",id,source,region) model = YOLO('./best.pt') print("start loaded model!!!") @@ -92,7 +95,7 @@ def worker(id,source,region=None,stream=False): print("Error opening video stream....") break continue - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) + result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%device_count) use += " track:"+str((time.time()*1000) - bgn) del(ret) del(frame) From a4af74a7cbb974436fec636b595f17eb37dc4c40 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Sat, 23 Sep 2023 00:47:15 +0800 Subject: [PATCH 32/39] device_count --- app.py | 4 +++- show.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index c38a584..a1a156d 100644 --- a/app.py +++ b/app.py @@ -14,6 +14,7 @@ from multiprocessing import Process, Event app = Flask(__name__) +device_count=0 workers = {} @app.route('/start/') @@ -62,7 +63,6 @@ def worker(id,source,region=None,stream=False): region = 10 else: region = int(region) - device_count = torch.cuda.device_count() print("GPU device count:", device_count) print("start loading model...",id,source,region) model = YOLO('./best.pt') @@ -95,6 +95,7 @@ def worker(id,source,region=None,stream=False): print("Error opening video stream....") break continue + print("device_count",int(id)%device_count) result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%device_count) use += " track:"+str((time.time()*1000) - bgn) del(ret) @@ -190,6 +191,7 @@ def post_request(url, data): # break if __name__ == '__main__': + device_count = torch.cuda.device_count() # cProfile.run('test_function()', 'test_function.profile') # p = pstats.Stats('test_function.profile') # p.sort_stats('cumulative').print_stats(100) # Top 10 by cumulative time diff --git a/show.py b/show.py index 320a539..38224bc 100644 --- a/show.py +++ b/show.py @@ -25,6 +25,16 @@ def show(): print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) return 'ok' +@app.route('/mj',methods=['POST']) +def mj(): + print(request.json) + return 'ok' + +@app.route('/cq',methods=['POST']) +def cq(): + print(request.json) + return 'ok' + if __name__ == '__main__': app.logger.setLevel(logging.ERROR) app.run("0.0.0.0",port=8000,threaded=True) From 1dab8ae3e18acb6691c62eb2d6abaaa1df2048bf Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Sat, 23 Sep 2023 00:48:48 +0800 Subject: [PATCH 33/39] 123 --- app.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app.py b/app.py index a1a156d..e085d4d 100644 --- a/app.py +++ b/app.py @@ -14,7 +14,7 @@ from multiprocessing import Process, Event app = Flask(__name__) -device_count=0 +device_count = torch.cuda.device_count() workers = {} @app.route('/start/') @@ -191,7 +191,6 @@ def post_request(url, data): # break if __name__ == '__main__': - device_count = torch.cuda.device_count() # cProfile.run('test_function()', 'test_function.profile') # p = pstats.Stats('test_function.profile') # p.sort_stats('cumulative').print_stats(100) # Top 10 by cumulative time From c496ffc1ba740f8df315e910e80b31aa565f0f95 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Sat, 23 Sep 2023 00:49:42 +0800 Subject: [PATCH 34/39] 4 --- app.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/app.py b/app.py index e085d4d..a7bba1c 100644 --- a/app.py +++ b/app.py @@ -14,7 +14,6 @@ from multiprocessing import Process, Event app = Flask(__name__) -device_count = torch.cuda.device_count() workers = {} @app.route('/start/') @@ -63,7 +62,6 @@ def worker(id,source,region=None,stream=False): region = 10 else: region = int(region) - print("GPU device count:", device_count) print("start loading model...",id,source,region) model = YOLO('./best.pt') print("start loaded model!!!") @@ -95,8 +93,7 @@ def worker(id,source,region=None,stream=False): print("Error opening video stream....") break continue - print("device_count",int(id)%device_count) - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%device_count) + result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) use += " track:"+str((time.time()*1000) - bgn) del(ret) del(frame) From ca1333bafb3b25d06fcbb097087c987fa60f038b Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Mon, 25 Sep 2023 14:51:14 +0800 Subject: [PATCH 35/39] sleep for mp4 --- app.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app.py b/app.py index a7bba1c..c706040 100644 --- a/app.py +++ b/app.py @@ -67,12 +67,21 @@ def worker(id,source,region=None,stream=False): print("start loaded model!!!") balls = {} count = 0 + delay = 0 + bgn = 0 try: cap = cv2.VideoCapture(source) if not cap.isOpened(): print("Error opening video stream.") + if source.endswith(".mp4"): + fps = cap.get(cv2.CAP_PROP_FPS) + delay = 1000.0 / fps + print("start video stream... ", fps, delay) while True: use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + if bgn != 0 and delay != 0 and time.time()*1000 - bgn < delay: + print("sleep",delay - (time.time()*1000 - bgn)) + time.sleep((delay - (time.time()*1000 - bgn))/1000) bgn = (time.time()*1000) ret, frame = cap.read() del(ret) From 27b6575ec93f4ff92516dd24206e76b414292ad2 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Mon, 25 Sep 2023 14:59:37 +0800 Subject: [PATCH 36/39] 2 --- app.py | 6 +-- show.py | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 120 insertions(+), 5 deletions(-) diff --git a/app.py b/app.py index c706040..d905791 100644 --- a/app.py +++ b/app.py @@ -79,9 +79,9 @@ def worker(id,source,region=None,stream=False): print("start video stream... ", fps, delay) while True: use = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - if bgn != 0 and delay != 0 and time.time()*1000 - bgn < delay: - print("sleep",delay - (time.time()*1000 - bgn)) - time.sleep((delay - (time.time()*1000 - bgn))/1000) + if bgn != 0 and delay != 0 and time.time()*1000 - bgn < 2*delay: + print("sleep",2*delay - (time.time()*1000 - bgn)) + time.sleep((2*delay - (time.time()*1000 - bgn))/1000) bgn = (time.time()*1000) ret, frame = cap.read() del(ret) diff --git a/show.py b/show.py index 38224bc..7de9e48 100644 --- a/show.py +++ b/show.py @@ -24,17 +24,132 @@ def show(): text += f"| {i} : {ball} " print(datetime.now().strftime('%Y-%m-%d %H:%M:%S'),text) return 'ok' - +import re @app.route('/mj',methods=['POST']) def mj(): print(request.json) + return 'ok' @app.route('/cq',methods=['POST']) def cq(): - print(request.json) + # print(request.json) + if request.json["post_type"] != "message": + return 'ok' + if request.json["message_type"] != "group": + return 'ok' + if request.json["group_id"] != "116277572": + return 'ok' + print(request.json['raw_message']) + + # 正则表达式来匹配URL + url_pattern = re.compile(r'url=(.*?)\]') + + # 使用findall来找到所有的URL + urls = re.findall(url_pattern, request.json['raw_message']) + + base64Array = [] + # 输出找到的URL + for url in urls: + print(url) + + + # 从URL下载图片 + response = requests.get(url) + response.raise_for_status() # 如果请求失败,此处将引发异常 + + # 将图片数据转换为Base64编码的字节数组 + image_base64_bytes = base64.b64encode(response.content) + + # 如果您需要将其转换为字符串,您可以使用.decode('utf-8')将字节数组转换为字符串 + image_base64_str = image_base64_bytes.decode('utf-8') + + # 打印Base64编码的字节数组 + # print(image_base64_bytes) + base64Array.append('data:image/jpeg;base64,'+image_base64_str) + + if len(base64Array) == 0: + return 'ok' + # 示例调用 + params = { + 'base64Array':base64Array, + 'notifyHook': 'http://hk.luanhailiang.cn:5000/mj', + 'prompt': 'game assets, game ui, animal, plant, cute, soft, baby, magical, forest, spirit, fairy', + 'state': '' + } + + response = imagine(params) + print(response.json()) + return 'ok' +import base64 +import requests + + +def imagine(params): + """ + 提交Imagine任务 + + :param params: imagineDTO,一个字典,包含以下键: + - base64Array: 垫图base64数组 + - notifyHook: 回调地址, 为空时使用全局notifyHook + - prompt: 提示词 + - state: 自定义参数 + :return: 返回服务器的响应 + """ + # API端点 + url = 'http://127.0.0.1:8080/mj/submit/imagine' + + # 发送POST请求 + response = requests.post(url, json=params) + + # 如果需要,处理响应(例如,检查响应状态,解析响应主体等) + + return response + if __name__ == '__main__': + + # # 这是包含URL的示例文本 + # text = '[CQ:image,file=42c93be3dbe10bfa79ad93ce1baed561.image,subType=1,url=https://gchat.qpic.cn/gchatpic_new/1183464602/691534145-2408237736-42C93BE3DBE10BFA79AD93CE1BAED561/0?term=2&is_origin=0][CQ:image,file=42c93be3dbe10bfa79ad93ce1baed561.image,subType=1,url=https://gchat.qpic.cn/gchatpic_new/1183464602/691534145-2408237736-42C93BE3DBE10BFA79AD93CE1BAED561/0?term=2&is_origin=0]' + + # # 正则表达式来匹配URL + # url_pattern = re.compile(r'url=(.*?)\]') + + + + # # 使用findall来找到所有的URL + # urls = re.findall(url_pattern, text) + + # # 输出找到的URL + # for url in urls: + # print(url) + + + + # # 从URL下载图片 + # response = requests.get(url) + # response.raise_for_status() # 如果请求失败,此处将引发异常 + + # # 将图片数据转换为Base64编码的字节数组 + # image_base64_bytes = base64.b64encode(response.content) + + # # 如果您需要将其转换为字符串,您可以使用.decode('utf-8')将字节数组转换为字符串 + # image_base64_str = image_base64_bytes.decode('utf-8') + + # # 打印Base64编码的字节数组 + # # print(image_base64_bytes) + + # # 示例调用 + # params = { + # 'base64Array': ['data:image/jpeg;base64,'+image_base64_str], + # 'notifyHook': 'http://hk.luanhailiang.cn:5000/mj', + # 'prompt': 'character design, game assets, game ui, cute, soft', + # 'state': '' + # } + + # response = imagine(params) + # print(response.json()) + app.logger.setLevel(logging.ERROR) app.run("0.0.0.0",port=8000,threaded=True) From e1c0e166197fdfc0f186f47ccf24f6ecc3453733 Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Tue, 26 Sep 2023 18:39:50 +0800 Subject: [PATCH 37/39] tracker --- app.py | 2 +- botsort.yaml | 18 +++++++++ show.py | 106 +++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 botsort.yaml diff --git a/app.py b/app.py index d905791..68f05bb 100644 --- a/app.py +++ b/app.py @@ -102,7 +102,7 @@ def worker(id,source,region=None,stream=False): print("Error opening video stream....") break continue - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4) + result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4,tracker='botsort.yaml') use += " track:"+str((time.time()*1000) - bgn) del(ret) del(frame) diff --git a/botsort.yaml b/botsort.yaml new file mode 100644 index 0000000..83be25a --- /dev/null +++ b/botsort.yaml @@ -0,0 +1,18 @@ +# Ultralytics YOLO 🚀, AGPL-3.0 license +# Default YOLO tracker settings for BoT-SORT tracker https://github.com/NirAharon/BoT-SORT + +tracker_type: botsort # tracker type, ['botsort', 'bytetrack'] +track_high_thresh: 0.5 # threshold for the first association +track_low_thresh: 0.1 # threshold for the second association +new_track_thresh: 0.6 # threshold for init new track if the detection does not match any tracks +track_buffer: 30 # buffer to calculate the time when to remove tracks +match_thresh: 0.8 # threshold for matching tracks +# min_box_area: 10 # threshold for min box areas(for tracker evaluation, not used for now) +# mot20: False # for tracker evaluation(not used for now) + +# BoT-SORT settings +gmc_method: sparseOptFlow # method of global motion compensation +# ReID model related thresh (not supported yet) +proximity_thresh: 0.5 +appearance_thresh: 0.25 +with_reid: False \ No newline at end of file diff --git a/show.py b/show.py index 7de9e48..760001f 100644 --- a/show.py +++ b/show.py @@ -2,6 +2,7 @@ import cv2 import json import time import logging +import threading import requests import supervision as sv @@ -28,32 +29,48 @@ import re @app.route('/mj',methods=['POST']) def mj(): print(request.json) - + if request.json["progress"] == "100%": + print(request.json["imageUrl"]) + sendmsg(request.json["imageUrl"]) + download_image(request.json["imageUrl"], "MJ", request.json["state"]) return 'ok' @app.route('/cq',methods=['POST']) def cq(): - # print(request.json) + print(request.json) if request.json["post_type"] != "message": return 'ok' if request.json["message_type"] != "group": return 'ok' - if request.json["group_id"] != "116277572": + if request.json["group_id"] != 116277572: return 'ok' - print(request.json['raw_message']) + # if request.json["user_id"] != 2132960: + # return 'ok' + + thread = threading.Thread(target=handle, args=(request.json,)) + thread.start() + return "ok" + +def handle(data): + print(data['raw_message']) # 正则表达式来匹配URL url_pattern = re.compile(r'url=(.*?)\]') # 使用findall来找到所有的URL - urls = re.findall(url_pattern, request.json['raw_message']) + urls = re.findall(url_pattern, data['raw_message']) + + result = re.sub(r'\[.*?\]', '', data['raw_message']) + result = result.replace('\r\n', '') + + name = data["sender"]["card"] or data["sender"]["nickname"] base64Array = [] # 输出找到的URL for url in urls: print(url) - + download_image(url, "QQ", name) # 从URL下载图片 response = requests.get(url) response.raise_for_status() # 如果请求失败,此处将引发异常 @@ -70,19 +87,70 @@ def cq(): if len(base64Array) == 0: return 'ok' + prompt = 'game assets,game ui,animal,flower,plant,forest,cute,baby,magical,spirit,fairy,elf,glowing light,anime style,seeming very happy,from above,ultra detailed,soft,on white background' + if result != '': + prompt = result # 示例调用 params = { 'base64Array':base64Array, 'notifyHook': 'http://hk.luanhailiang.cn:5000/mj', - 'prompt': 'game assets, game ui, animal, plant, cute, soft, baby, magical, forest, spirit, fairy', - 'state': '' + 'prompt': prompt, + 'state': name } + response = imagine(params) + print(response.json()) + time.sleep(3) + params['prompt'] = prompt + " --iw 2 --niji 5" + params['state'] = name+"_niji" + response = imagine(params) + print(response.json()) + time.sleep(3) + params['prompt'] = prompt + " --iw 2 --style raw" + params['state'] = name+"_raw" response = imagine(params) print(response.json()) return 'ok' +def download_image(url, t, name): + """ + 从指定URL下载图片并保存到本地文件。 + + :param url: 图片的URL。 + :param local_filename: 要保存图片的本地文件名。 + """ + + local_filename = f"y:\美术\参考收集\{t}\{datetime.now().strftime('%Y%m%d%H%M%S')}_{name}" + response = requests.get(url) + if response.status_code == 200: # HTTP状态码200表示请求成功 + # 获取Content-Type响应头 + content_type = response.headers['Content-Type'] + # 判断图片格式 + ext = "" + if 'jpeg' in content_type or 'jpg' in content_type: + ext = '.jpg' + print('The image is in JPEG format.') + elif 'png' in content_type: + ext = '.png' + print('The image is in PNG format.') + elif 'gif' in content_type: + ext = '.gif' + print('The image is in GIF format.') + elif 'webp' in content_type: + ext = '.webp' + print('The image is in WEBP format.') + else: + ext = '.png' + print(f'Unknown image format: {content_type}') + local_filename += ext + with open(local_filename, 'wb') as f: + f.write(response.content) + print(f"Image successfully downloaded to {local_filename}") + else: + print(f"Error downloading image, HTTP status code: {response.status_code}") + + import base64 import requests @@ -108,6 +176,24 @@ def imagine(params): return response + +def sendmsg(url): + # 设置 go-cqhttp 服务的 URL 和端口 + api_url = 'http://localhost:5700/send_msg' + + # 设置要发送的消息参数 + data = { + 'group_id': 5464741, # 接收消息的用户的 QQ 号 + 'message_type': 'group', # 消息类型 + 'message': f'[CQ:image,file={url}]' # 消息内容,使用 CQ码 格式 + } + + # 发送 POST 请求到 go-cqhttp 服务 + response = requests.post(api_url, data=data) + + # 输出响应 + print(response.json()) + if __name__ == '__main__': # # 这是包含URL的示例文本 @@ -151,5 +237,9 @@ if __name__ == '__main__': # response = imagine(params) # print(response.json()) + + + + app.logger.setLevel(logging.ERROR) app.run("0.0.0.0",port=8000,threaded=True) From 226cccbcf6dcec2b275ed2bae25ebf881c08ab5a Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Tue, 26 Sep 2023 19:07:59 +0800 Subject: [PATCH 38/39] conf --- app.py | 2 +- botsort.yaml | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app.py b/app.py index 68f05bb..665d848 100644 --- a/app.py +++ b/app.py @@ -102,7 +102,7 @@ def worker(id,source,region=None,stream=False): print("Error opening video stream....") break continue - result = model.track(frame,show=False,stream=False,persist=True,device=int(id)%4,tracker='botsort.yaml') + result = model.track(frame,device=int(id)%4,tracker='botsort.yaml') use += " track:"+str((time.time()*1000) - bgn) del(ret) del(frame) diff --git a/botsort.yaml b/botsort.yaml index 83be25a..eba8d6b 100644 --- a/botsort.yaml +++ b/botsort.yaml @@ -6,13 +6,13 @@ track_high_thresh: 0.5 # threshold for the first association track_low_thresh: 0.1 # threshold for the second association new_track_thresh: 0.6 # threshold for init new track if the detection does not match any tracks track_buffer: 30 # buffer to calculate the time when to remove tracks -match_thresh: 0.8 # threshold for matching tracks +match_thresh: 0.3 # threshold for matching tracks # min_box_area: 10 # threshold for min box areas(for tracker evaluation, not used for now) # mot20: False # for tracker evaluation(not used for now) # BoT-SORT settings gmc_method: sparseOptFlow # method of global motion compensation # ReID model related thresh (not supported yet) -proximity_thresh: 0.5 +proximity_thresh: 0.2 appearance_thresh: 0.25 with_reid: False \ No newline at end of file From ff85571b470516ec28b7060da479b9434e685d2b Mon Sep 17 00:00:00 2001 From: luanhailiang Date: Tue, 26 Sep 2023 20:07:44 +0800 Subject: [PATCH 39/39] config test --- botsort.yaml | 10 +++++----- ez.py | 40 ++++++++++++++++++++++++++++++++++++++++ test.py | 2 +- 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 ez.py diff --git a/botsort.yaml b/botsort.yaml index eba8d6b..922a5c0 100644 --- a/botsort.yaml +++ b/botsort.yaml @@ -2,17 +2,17 @@ # Default YOLO tracker settings for BoT-SORT tracker https://github.com/NirAharon/BoT-SORT tracker_type: botsort # tracker type, ['botsort', 'bytetrack'] -track_high_thresh: 0.5 # threshold for the first association +track_high_thresh: 0.3 # threshold for the first association track_low_thresh: 0.1 # threshold for the second association -new_track_thresh: 0.6 # threshold for init new track if the detection does not match any tracks -track_buffer: 30 # buffer to calculate the time when to remove tracks -match_thresh: 0.3 # threshold for matching tracks +new_track_thresh: 0.5 # threshold for init new track if the detection does not match any tracks +track_buffer: 10 # buffer to calculate the time when to remove tracks +match_thresh: 0.8 # threshold for matching tracks # min_box_area: 10 # threshold for min box areas(for tracker evaluation, not used for now) # mot20: False # for tracker evaluation(not used for now) # BoT-SORT settings gmc_method: sparseOptFlow # method of global motion compensation # ReID model related thresh (not supported yet) -proximity_thresh: 0.2 +proximity_thresh: 0.1 appearance_thresh: 0.25 with_reid: False \ No newline at end of file diff --git a/ez.py b/ez.py new file mode 100644 index 0000000..b5e490e --- /dev/null +++ b/ez.py @@ -0,0 +1,40 @@ +import cv2 +import time + +# 获取 ezopen 协议的 URL +url = "rtmp://rtmp03open.ys7.com:1935/v3/openlive/L05874022_1_1?expire=1726487667&id=625419662355599360&t=228dca5d37b6bead201e80cca3fc5fe74140242278e4eb51a99bcce593cae027&ev=100" + +# 创建 VideoCapture 对象 +cap = cv2.VideoCapture(url) + +cv2.namedWindow("Video", cv2.WINDOW_NORMAL) # Create a named window +cv2.resizeWindow("Video", 640, 384) # Resize this window + +start_time = time.time() +count = 0 + +# 获取视频帧 +while True: + ret, frame = cap.read() + if time.time() - start_time < 1: # 循环运行直到1秒过去 + count += 1 + else: + print(count) + start_time = time.time() + count = 0 + + # print(ret) + if not ret: + print("Error read video stream.") + break + # 显示视频帧 + cv2.imshow("Video", frame) + # print("0000000000000",ret) + # 等待用户按下任意键退出 + key = cv2.waitKey(1) + if key == 27: + break + +# 释放资源 +cap.release() +cv2.destroyAllWindows() diff --git a/test.py b/test.py index 77920ff..d403e08 100644 --- a/test.py +++ b/test.py @@ -2,4 +2,4 @@ from ultralytics import YOLO # Configure the tracking parameters and run the tracker model = YOLO('best.pt') -results = model.track(source="./videos/123.mp4", conf=0.3, iou=0.5, show=True) +results = model.track(source="./videos/123.mp4", tracker='botsort.yaml', show=True)