From 28c7097af424d7f067c9e1f20d2304c9ecf9f22f Mon Sep 17 00:00:00 2001 From: xszyou Date: Mon, 28 Oct 2024 19:27:44 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B4=A7=E6=80=A5=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新依赖 --- ai_module/yolov8.py | 148 ------------------------------------------- core/fay_core.py | 3 - llm/nlp_VisualGLM.py | 37 ----------- requirements.txt | 4 +- 4 files changed, 3 insertions(+), 189 deletions(-) delete mode 100644 ai_module/yolov8.py delete mode 100644 llm/nlp_VisualGLM.py diff --git a/ai_module/yolov8.py b/ai_module/yolov8.py deleted file mode 100644 index d20b799..0000000 --- a/ai_module/yolov8.py +++ /dev/null @@ -1,148 +0,0 @@ -from ultralytics import YOLO -from scipy.spatial import procrustes -import numpy as np -import cv2 -import time -from scheduler.thread_manager import MyThread - -__fei_eyes = None -class FeiEyes: - - def __init__(self): - - """ - 鼻子(0) - 左眼(1),右眼(2) - 左耳(3),右耳(4) - 左肩(5),右肩(6) - 左肘(7),右肘(8) - 左腕(9),右腕(10) - 左髋(11),右髋(12) - 左膝(13),右膝(14) - 左脚踝(15),右脚踝(16) - """ - self.POSE_PAIRS = [ - (3, 5), (5, 6), # upper body - (5, 7), (6, 8), (7, 9), (8, 10), # lower body - (11, 12), (11, 13), (12, 14), (13, 15) # arms - ] - self.my_face = np.array([[154.4565, 193.7006], - [181.8575, 164.8366], - [117.1820, 164.3602], - [213.5605, 193.0460], - [ 62.7056, 193.5217]]) - self.is_running = False - self.img = None - - def is_sitting(self, keypoints): - if len(keypoints) < 17: # 确保有足够的关键点 - return False - # 检查每个关键点的置信度 - if keypoints[11][2] < 0.5 or keypoints[12][2] < 0.5 or keypoints[13][2] < 0.5 or keypoints[14][2] < 0.5 or keypoints[15][2] < 0.5 or keypoints[16][2] < 0.5: - return False - - left_hip, right_hip = keypoints[11][:2], keypoints[12][:2] - left_knee, right_knee = keypoints[13][:2], keypoints[14][:2] - left_ankle, right_ankle = keypoints[15][:2], keypoints[16][:2] - - hip_knee_y = (left_hip[1] + right_hip[1] + left_knee[1] + right_knee[1]) / 4 - knee_ankle_y = (left_knee[1] + right_knee[1] + left_ankle[1] + right_ankle[1]) / 4 - - return hip_knee_y < knee_ankle_y - - def is_standing(self, keypoints): - if len(keypoints) < 17 or keypoints[0][2] < 0.5 or keypoints[15][2] < 0.5 or keypoints[16][2] < 0.5: - return False - - head = keypoints[0][:2] - left_ankle, right_ankle = keypoints[15][:2], keypoints[16][:2] - - return head[1] > left_ankle[1] and head[1] > right_ankle[1] - - def get_counts(self): - if not self.is_running: - return 0,0,0 - return self.person_count, self.stand_count, self.sit_count - - def get_status(self): - return self.is_running - - def get_img(self): - if self.is_running: - return self.img - else: - return None - - def start(self): - cap = cv2.VideoCapture(0) - if cap.isOpened(): - self.is_running = True - MyThread(target=self.run, args=[cap]).start() - - def stop(self): - self.is_running = False - - def run(self, cap): - model = YOLO("yolov8n-pose.pt") - while self.is_running: - time.sleep(0.033) - ret, frame = cap.read() - self.img = frame - operated_frame = frame.copy() - if not ret: - break - results = model.predict(operated_frame, verbose=False) - person_count = 0 - sit_count = 0 - stand_count = 0 - for res in results: # loop over results - for box, cls in zip(res.boxes.xyxy, res.boxes.cls): # loop over detections - x1, y1, x2, y2 = box - cv2.rectangle(operated_frame, (int(x1.item()), int(y1.item())), (int(x2.item()), int(y2.item())), (0, 255, 0), 2) - cv2.putText(operated_frame, f"{res.names[int(cls.item())]}", (int(x1.item()), int(y1.item()) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) - if res.keypoints is not None and res.keypoints.xy.numel() > 0: # check if keypoints exist - keypoints = res.keypoints[0] - #总人数 - person_count += 1 - #坐着的人数 - if self.is_sitting(keypoints): - sit_count += 1 - #站着的人数 - elif self.is_standing(keypoints): - stand_count += 1 - - for keypoint in keypoints: # loop over keypoints - - if len(keypoint) == 3: - x, y, conf = keypoint - if conf > 0.5: # draw keypoints with confidence greater than 0.5 - cv2.circle(operated_frame, (int(x.item()), int(y.item())), 3, (0, 0, 255), -1) - - # Draw lines connecting keypoints - for pair in self.POSE_PAIRS: - if pair[0] < len(keypoints) and pair[1] < len(keypoints): - pt1, pt2 = keypoints[pair[0]][:2], keypoints[pair[1]][:2] - conf1, conf2 = keypoints[pair[0]][2], keypoints[pair[1]][2] - if conf1 > 0.5 and conf2 > 0.5: - # cv2.line(operated_frame, (int(pt1[0].item()), int(pt1[1].item())), (int(pt2[0].item()), int(pt2[1].item())), (255, 255, 0), 2) - pass - self.person_count = person_count - self.sit_count = sit_count - self.stand_count = stand_count - cv2.imshow("YOLO v8 Fay Eyes", operated_frame) - cv2.waitKey(1) - - cap.release() - cv2.destroyAllWindows() - - -def new_instance(): - global __fei_eyes - if __fei_eyes is None: - __fei_eyes = FeiEyes() - return __fei_eyes - - - - - diff --git a/core/fay_core.py b/core/fay_core.py index 16587e9..bb5c963 100644 --- a/core/fay_core.py +++ b/core/fay_core.py @@ -24,8 +24,6 @@ from core import content_db from ai_module import nlp_cemotion from llm import nlp_rasa from llm import nlp_gpt -from ai_module import yolov8 -from llm import nlp_VisualGLM from llm import nlp_lingju from llm import nlp_xingchen from llm import nlp_langchain @@ -58,7 +56,6 @@ if platform.system() == "Windows": modules = { "nlp_gpt": nlp_gpt, "nlp_rasa": nlp_rasa, - "nlp_VisualGLM": nlp_VisualGLM, "nlp_lingju": nlp_lingju, "nlp_xingchen": nlp_xingchen, "nlp_langchain": nlp_langchain, diff --git a/llm/nlp_VisualGLM.py b/llm/nlp_VisualGLM.py deleted file mode 100644 index 205c1c2..0000000 --- a/llm/nlp_VisualGLM.py +++ /dev/null @@ -1,37 +0,0 @@ -""" -这是对于清华智谱VisualGLM-6B的代码,在使用前请先安装并启动好VisualGLM-6B. -https://github.com/THUDM/VisualGLM-6B -""" -import json -import requests -import uuid -import os -import cv2 -from ai_module import yolov8 - -# Initialize an empty history list -communication_history = [] - -def question(cont, uid=0): - if not yolov8.new_instance().get_status(): - return "请先启动“Fay Eyes”" - content = { - "text":cont, - "history":communication_history} - img = yolov8.new_instance().get_img() - if yolov8.new_instance().get_status() and img is not None: - filename = str(uuid.uuid4()) + ".jpg" - current_working_directory = os.getcwd() - filepath = os.path.join(current_working_directory, "data", filename) - cv2.imwrite(filepath, img) - content["image"] = filepath - url = "http://127.0.0.1:8080" - print(content) - req = json.dumps(content) - headers = {'content-type': 'application/json'} - r = requests.post(url, headers=headers, data=req) - - # Save this conversation to history - communication_history.append([cont, r.text]) - - return r.text + "\n(相片:" + filepath + ")" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3f5e15e..107057e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,4 +22,6 @@ chromadb tenacity==8.2.3 pygame scipy -flask-httpauth \ No newline at end of file +flask-httpauth +opencv-python +psutil \ No newline at end of file