Skip to content

Commit

Permalink
Upload 5.7.1.1 alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
Ender-William committed Mar 2, 2024
1 parent 9918e6a commit 8feccc5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 50 deletions.
45 changes: 34 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ GitHub 地址:[YOLOv5 🚀 是世界上最受欢迎的视觉 AI,代表 Ultra

# Section 3 如何安装到 Python 环境

`whl` 文件夹或者从`Release`下载 `yolo_detectAPI-5.7-py3-none-any.whl` ,在下载目录内进入 Terminal 并切换至你要安装的 Python 环境。输入下面的命令安装 Python 库。这里需要注意,Python 环境需要 3.8 及以上版本才能使用。
`whl` 文件夹或者从`Release`下载 `yolo_detectAPI-5.7.1.1-py3-none-any.whl` ,在下载目录内进入 Terminal 并切换至你要安装的 Python 环境。输入下面的命令安装 Python 库。这里需要注意,Python 环境需要 3.8 及以上版本才能使用。

```shell
pip install .\yolo_detectAPI-5.7-py3-none-any.whl
pip install .\yolo_detectAPI-5.7.1.1-py3-none-any.whl
```

这个库使用 CPU 执行程序,如果需要使用 GPU 执行程序请 clone 源码自行打包修改程序。
Expand All @@ -53,25 +53,45 @@ python setup.py sdist bdist_wheel

```python
import cv2
import yolo_detectAPI
import torch
import time

from yolo_detectAPI import DetectAPI


if __name__ == '__main__':
cap = cv2.VideoCapture(0)
a = yolo_detectAPI.DetectAPI(weights='last.pt', conf_thres=0.5, iou_thres=0.5) # 你要使用的模型的路径

camera_index = 0
capture = cv2.VideoCapture(camera_index)

yolo_api = DetectAPI(
weights='weights/best.pt',
device='0',
conf_thres=0.4, iou_thres=0.1,
half=False)

with torch.no_grad():
while True:
rec, img = cap.read()
result, names = a.detect([img])
img = result[0][0] # 每一帧图片的处理结果图片
ret, frame = capture.read()
if not ret:
continue
start_time = time.time()

result, names = yolo_api.detect([frame])
drew_image = result[0][0] # 每一帧图片的处理结果图片
# 每一帧图像的识别结果(可包含多个物体)
for cls, (x1, y1, x2, y2), conf in result[0][1]:
print(names[cls], x1, y1, x2, y2, conf) # 识别物体种类、左上角x坐标、左上角y轴坐标、右下角x轴坐标、右下角y轴坐标,置信度

cv2.imshow("video", img)
print(f"name: {names[cls]}, x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}, conf: {conf}") # 识别物体种类、左上角x坐标、左上角y轴坐标、右下角x轴坐标、右下角y轴坐标,置信度
'''
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0))
cv2.putText(img,names[cls],(x1,y1-20),cv2.FONT_HERSHEY_DUPLEX,1.5,(255,0,0))
'''
# print() # 将每一帧的结果输出分开
cv2.imshow("drew_image", drew_image)

if cv2.waitKey(1) == ord('q'):
break
print(f"Infer FPS: {1/(time.time() - start_time)}")
```


Expand Down Expand Up @@ -101,6 +121,9 @@ https://github.com/ultralytics/yolov5/releases/tag/v7.0
https://blog.csdn.net/weixin_51331359/article/details/126012620
https://blog.csdn.net/CharmsLUO/article/details/123422822

# Update Version 5.7.1.1 2024-03-02
添加了 GPU 支持

# Update Version 5.7.1 2023-03-29
添加了 `conf_thres``iou_thres` 的设置方法,在初始化识别方法时可以添加。
```python
Expand Down
31 changes: 22 additions & 9 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,37 @@

from yolo_detectAPI import DetectAPI


if __name__ == '__main__':
cap = cv2.VideoCapture(0)
a = DetectAPI(weights='weights/best.pt', device='0', conf_thres=0.4, iou_thres=0.1)

camera_index = 0
capture = cv2.VideoCapture(camera_index)

yolo_api = DetectAPI(
weights='weights/best.pt',
device='0',
conf_thres=0.4, iou_thres=0.1,
half=False)

with torch.no_grad():
while True:
rec, img = cap.read()
ret, frame = capture.read()
if not ret:
continue
start_time = time.time()
result, names = a.detect([img])
img = result[0][0] # 每一帧图片的处理结果图片

result, names = yolo_api.detect([frame])
drew_image = result[0][0] # 每一帧图片的处理结果图片
# 每一帧图像的识别结果(可包含多个物体)
for cls, (x1, y1, x2, y2), conf in result[0][1]:
# print(names[cls], x1, y1, x2, y2, conf) # 识别物体种类、左上角x坐标、左上角y轴坐标、右下角x轴坐标、右下角y轴坐标,置信度
print(f"name: {names[cls]}, x1: {x1}, y1: {y1}, x2: {x2}, y2: {y2}, conf: {conf}") # 识别物体种类、左上角x坐标、左上角y轴坐标、右下角x轴坐标、右下角y轴坐标,置信度
'''
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0))
cv2.putText(img,names[cls],(x1,y1-20),cv2.FONT_HERSHEY_DUPLEX,1.5,(255,0,0))'''
cv2.putText(img,names[cls],(x1,y1-20),cv2.FONT_HERSHEY_DUPLEX,1.5,(255,0,0))
'''
# print() # 将每一帧的结果输出分开
cv2.imshow("video", img)
cv2.imshow("drew_image", drew_image)

if cv2.waitKey(1) == ord('q'):
break
print(f"FPS: {1/(time.time() - start_time)}")
print(f"Infer FPS: {1/(time.time() - start_time)}")
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='yolo_detectAPI',
version='5.7.2',
version='5.7.1.1',
description='Detect API',
long_description='This is a API for yolov5 version7 detect.py, new-version 5.7.1 allow user set <conf_thres> and '
'<iou_thres>',
Expand All @@ -15,7 +15,7 @@
pakages=['yolo_detectAPI'],
include_package_data=True,
readme = 'README.md',
python_requires='>=3.7',
python_requires='>=3.8',
url = 'http://blogs.kd-mercury.xyz/',
install_requires=['matplotlib>=3.2.2', 'numpy>=1.18.5', 'opencv-python>=4.1.1',
'Pillow>=7.1.2', 'PyYAML>=5.3.1', 'requests>=2.23.0', 'scipy>=1.4.1',
Expand All @@ -24,6 +24,7 @@
'ipython>=8.3.0', 'psutil>=5.9.4'],
data_files=['export.py'],
classifiers=[
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
Expand Down
42 changes: 14 additions & 28 deletions yolo_detectAPI/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,17 @@ def __init__(self, weights='weights/last.pt',


class DetectAPI:
def __init__(self, weights, imgsz=(640,640), device=None, conf_thres=None, iou_thres=None):
def __init__(self, weights:str, imgsz:tuple=(640,640), device:chr=None,
conf_thres:float=None, iou_thres:float=None, half:bool=False):
"""
Init Detect API
Args:
weights: model
imgsz: default 640
conf_thres: 用于物体的识别率,object置信度阈值 默认0.25,大于此准确率才会显示识别结果
iou_thres: 用于去重,做nms的iou阈值 默认0.45,数值越小去重程度越高
weights(str): model path
imgsz(tuple): default 640
device(chr): cpu or 0, 1, 2, etc. for cuda
conf_thres(float): 用于物体的识别率,object置信度阈值 默认0.25,大于此准确率才会显示识别结果
iou_thres(float): 用于去重,做nms的iou阈值 默认0.45,数值越小去重程度越高
half(bool): 使用半精度进行推理(选用 CPU 时不起效)
"""
self.opt = YoloOpt(weights=weights, imgsz=imgsz)
if conf_thres is not None:
Expand All @@ -77,8 +80,11 @@ def __init__(self, weights, imgsz=(640,640), device=None, conf_thres=None, iou_t
else:
self.device = select_device(device)
# 不使用半精度
self.half = self.device.type != 'cpu' # # FP16 supported on limited backends with CUDA
self.half = False
# self.half = self.device.type != 'cpu' # # FP16 supported on limited backends with CUDA
# self.half = False

# 选择是否使用半精度
self.half = True if self.device.type != 'cpu' and half is True else False

# Load model 加载模型
self.model = DetectMultiBackend(weights, self.device, dnn=False, fp16=self.half)
Expand Down Expand Up @@ -130,7 +136,7 @@ def detect(self, source):

# Inference
with dt[1]:
self.visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if self.visualize else False
# self.visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if self.visualize else False
pred = self.model(im, augment=self.opt.augment, visualize=self.visualize)

# NMS
Expand Down Expand Up @@ -159,23 +165,3 @@ def detect(self, source):
result.append((im0, result_txt)) # 对于每张图片,返回画完框的图片,以及该图片的标签列表。
return result, self.names


if __name__ == '__main__':
cap = cv2.VideoCapture(0)
a = DetectAPI(weights='weights/last.pt', device='0')
with torch.no_grad():
while True:
rec, img = cap.read()
result, names = a.detect([img])
img = result[0][0] # 每一帧图片的处理结果图片
# 每一帧图像的识别结果(可包含多个物体)
for cls, (x1, y1, x2, y2), conf in result[0][1]:
print(names[cls], x1, y1, x2, y2, conf) # 识别物体种类、左上角x坐标、左上角y轴坐标、右下角x轴坐标、右下角y轴坐标,置信度
'''
cv2.rectangle(img,(x1,y1),(x2,y2),(0,255,0))
cv2.putText(img,names[cls],(x1,y1-20),cv2.FONT_HERSHEY_DUPLEX,1.5,(255,0,0))'''
print() # 将每一帧的结果输出分开
cv2.imshow("vedio", img)

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

0 comments on commit 8feccc5

Please sign in to comment.