Skip to content

Commit 5bc3060

Browse files
committed
2025/4/21
1 parent e9892f0 commit 5bc3060

File tree

4 files changed

+212
-1
lines changed

4 files changed

+212
-1
lines changed

a.py

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
"""
2+
Copyright (R) @huawei.com, all rights reserved
3+
-*- coding:utf-8 -*-
4+
CREATED: 2023-05-25 09:12:13
5+
MODIFIED: 2023-05-25 10:10:55
6+
"""
7+
8+
9+
# import videocapture as video
10+
import numpy as np
11+
import cv2
12+
import sys
13+
sys.path.append("/root/ACLLite/python") # 添加到文件开头
14+
import time
15+
16+
from acllite_resource import AclLiteResource
17+
from acllite_model import AclLiteModel
18+
from acllite_imageproc import AclLiteImageProc
19+
from acllite_image import AclLiteImage
20+
from acllite_logger import log_error, log_info
21+
22+
labels = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
23+
24+
class sampleYOLOV7(object):
25+
'''load the model, and do preprocess, infer, postprocess'''
26+
def __init__(self, model_path, model_width, model_height):
27+
self.model_path = model_path
28+
self.model_width = model_width
29+
self.model_height = model_height
30+
31+
def init_resource(self):
32+
# initial acl resource, create image processor, create model
33+
self._resource = AclLiteResource()
34+
self._resource.init()
35+
36+
self._dvpp = AclLiteImageProc(self._resource)
37+
self._model = AclLiteModel(self.model_path)
38+
39+
def preprocess(self, frame):
40+
# resize frame, keep original image
41+
self.src_image = frame
42+
self.resized_image = cv2.resize(frame, (self.model_width, self.model_height))
43+
self.resized_image = self.resized_image.transpose(2,0,1)[np.newaxis,:]
44+
45+
def infer(self):
46+
# infer frame
47+
self.result = self._model.execute([self.resized_image])
48+
49+
@staticmethod
50+
def nms(boxes, scores, iou_threshold=0.5):
51+
# 按置信度排序
52+
order = scores.argsort()[::-1]
53+
keep = []
54+
55+
while order.size > 0:
56+
# 保留置信度最高的框
57+
keep.append(order[0])
58+
if order.size == 1:
59+
break
60+
61+
# 计算IoU
62+
xx1 = np.maximum(boxes[order[0], 0], boxes[order[1:], 0])
63+
yy1 = np.maximum(boxes[order[0], 1], boxes[order[1:], 1])
64+
xx2 = np.minimum(boxes[order[0], 2], boxes[order[1:], 2])
65+
yy2 = np.minimum(boxes[order[0], 3], boxes[order[1:], 3])
66+
67+
w = np.maximum(0.0, xx2 - xx1)
68+
h = np.maximum(0.0, yy2 - yy1)
69+
inter = w * h
70+
71+
area1 = (boxes[order[0], 2] - boxes[order[0], 0]) * (boxes[order[0], 3] - boxes[order[0], 1])
72+
area2 = (boxes[order[1:], 2] - boxes[order[1:], 0]) * (boxes[order[1:], 3] - boxes[order[1:], 1])
73+
iou = inter / (area1 + area2 - inter)
74+
75+
# 删除IoU大于阈值的框
76+
inds = np.where(iou <= iou_threshold)[0]
77+
order = order[inds + 1]
78+
79+
return keep
80+
def postprocess(self):
81+
predictions = self.result[0]
82+
h, w, _ = self.src_image.shape
83+
scale_x = w / self.model_width
84+
scale_y = h / self.model_height
85+
86+
# 解析预测结果
87+
predictions = predictions.reshape(-1, predictions.shape[-1])
88+
confidences = predictions[:, 4]
89+
class_scores = predictions[:, 5:]
90+
class_ids = np.argmax(class_scores, axis=1)
91+
92+
# 修改: 只保留每个类别置信度最高的框
93+
keep_boxes = []
94+
for cls in np.unique(class_ids):
95+
cls_mask = (class_ids == cls)
96+
cls_boxes = predictions[cls_mask]
97+
cls_conf = confidences[cls_mask]
98+
99+
# 只保留置信度最高的一个框
100+
if len(cls_conf) > 0:
101+
highest_conf_idx = np.argmax(cls_conf)
102+
cls_idx = np.where(cls_mask)[0][highest_conf_idx]
103+
keep_boxes.append(cls_idx)
104+
105+
# 过滤低置信度
106+
keep_boxes = np.array(keep_boxes)
107+
final_mask = confidences[keep_boxes] > 0.70
108+
boxes = predictions[keep_boxes, :4][final_mask]
109+
confidences = confidences[keep_boxes][final_mask]
110+
class_ids = class_ids[keep_boxes][final_mask]
111+
112+
# 绘制检测结果
113+
for box, conf, cls_id in zip(boxes, confidences, class_ids):
114+
x1, y1, x2, y2 = (box * [scale_x, scale_y, scale_x, scale_y]).astype(int)
115+
label = f"{labels[cls_id]} {conf:.2f}"
116+
cv2.rectangle(self.src_image, (x1,y1), (x2,y2), (0,255,0), 2)
117+
cv2.putText(self.src_image, label, (x1, max(y1-20,10)),
118+
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,255,0), 2)
119+
120+
cv2.imshow('Detection', self.src_image)
121+
def release_resource(self):
122+
# release resource includes acl resource, data set and unload model
123+
del self._resource
124+
del self._dvpp
125+
del self._model
126+
del self.resized_image
127+
128+
def find_camera_index():
129+
max_index_to_check = 10 # Maximum index to check for camera
130+
for index in range(max_index_to_check):
131+
cap = cv2.VideoCapture(index)
132+
if cap.read()[0]:
133+
cap.release()
134+
return index
135+
# If no camera is found
136+
raise ValueError("No camera found.")
137+
138+
139+
if __name__ == '__main__':
140+
model_path = '/root/thuei-1/EdgeAndRobotics/Samples/YOLOV5USBCamera/model/numbers.om'
141+
model_width = 640
142+
model_height = 640
143+
model = sampleYOLOV7(model_path, model_width, model_height)
144+
model.init_resource()
145+
146+
# camera_index = find_camera_index()
147+
cap = cv2.VideoCapture(0)
148+
cv2.namedWindow('out', cv2.WINDOW_NORMAL)
149+
while True:
150+
ret, frame = cap.read()
151+
if not ret:
152+
print("Can't receive frame (stream end?). Exiting ...")
153+
break
154+
# print(f"图像形状: {frame.shape}")
155+
# frame = cv2.resize(frame, (640, 640))
156+
# print(f"图像形状: {frame.shape}")
157+
print(model.model_height,model.model_width, frame.shape, model.model_path)
158+
model.preprocess(frame)
159+
model.infer()
160+
model.postprocess()
161+
# cv2.imshow('Frame', frame)
162+
if cv2.waitKey(1) & 0xFF == ord('q'):
163+
break
164+
cap.release()
165+
cv2.destroyAllWindows()
166+
167+
model.release_resource()

docs/summary/2025/2025-W16-04.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: "2025-W16-04"
3+
tags:
4+
- "周记"
5+
categories: dairy
6+
date: "2025-04-14T22:27:05+08:00"
7+
modify: "2025-04-14T22:27:05+08:00"
8+
dir: dairy
9+
share: false
10+
cdate: "2025-04-14"
11+
mdate: " 2025-04-14 "
12+
---
13+
14+
# 2025-W16-04
15+
过去的几周一直处于一种低迷的状态。总是找在找一些诸如完成课内作业,准备期中复习的“杂事”来麻痹自己,把真正重要的学习与输入不断的往后拖延。以及不小心入坑了 bangdream,一段时间沉溺于补番与打歌之中(~~并不后悔~~)。
16+
其实这周的周结差点又要因为我的懒惰被 skip 掉。然而 4/20 晚上的⚾棒球比赛实在是过于激动人心,激动之余提醒我也是时候收拾一下过去的四周,为接下来的良好的学习循环做好准备。的 rx 队长的两发 homerun 以及完美的投球,守备,配球,打击
17+
## Review
18+
### THUEI-1
19+
一场酣畅淋漓的**
20+
记得熬了两次大夜(凌晨四点一次,清晨六点一次),现在想想其实是时间安排不太妥当,应当尽早开始路线硬编码的阶段,以及应当尽早调试夹取这个极为重要的得分操作。
21+
比较遗憾的是在凝聚了数据苦工们的血汗的数据集上训练出来的、花了十余个小时的训练与部署(时间全花在部署上了,香橙派上的 infer 代码并不适配 yolo 官方的原始输出的模型架构所,正式比赛前一天在 cursor 的伟大 infer 代码的拯救才证明了并不是我的模型训练问题而是 infer 代码的问题)的yolo 模型,最终赛前由于没有完成调试,并不能被 main 函数成功调用(被华为的 acwings 的 initialize 恶心到了),标志这我在这个比赛中付出的最大精力的一件事最终没有 work。也算是一次经历吧。
22+
### 学习读论文
23+
成功用飞书实现了一个自己读论文的流。开心😊。
24+
### 期中复习
25+
**模电****信号与系统**两门金课,以及**随统**三门专业课的复习。现在来看其实复习的过拟合程度有点过了。比较开心的事随统的附加题用到了许久未用的组合恒等式,拆了半天最后还是成功拆了出来😊。(~~沟槽的组合还在追我~~
26+
27+
### Vision and Robotics Lab
28+
感谢在看到系群的广告后勇于花一个下午做了一个非常简陋抽象的 cv 并且还恬不知耻的能投出去的自己,以及感谢看到如此简陋的 cv 后还能够接纳水平如此之低的我,给我一个 intern 的机会的 gy 老师,以及感谢 wx、jm、by 学长们的悉心指导。衷心期待这段经历能够有所收获。特别是在看完了 4/19 亦庄半程马拉松后,理解了学长所说的 loco 与 manipulation 的区别,即使现在还是在打杂的阶段,但也感觉到自己参与的项目也的确有实际的意义所在🫡。
29+
### Follow
30+
成功使用 follow 在一些平台上将想要关注的人通过 rss 将信息聚合到 follow 上(~~我会一直视奸你们👁️~~
31+
### kinnari 来京访问
32+
伟大的 kinnari 来京访问!kinnari 的恩情还不完!☀ ☀ ☀
33+
### 棒球
34+
伟大无需多言。伟大的 rx 队长回到了他忠诚的东棒球场☀ ☀ ☀ !请看队长的三分炮。
35+
!!! video
36+
summary/棒球.mp4
37+
### GenSI Open day
38+
记错时间了,结果踩着尾巴进去的。理解了人与人之间的差距究竟有多大。申请中译中:
39+
![d2c6b94524b80e3d39b2c71e477a7e9.jpg](https://raw.githubusercontent.com/Tendourisu/images/master/20250421213823619.png)
40+
41+
## THOUGHTS
42+
- 这几周学习的时间被各种挤占。期中结束后要重新将重点放到输入上。
43+
- 坚持记录。

docs/summary/棒球.mp4

3.33 MB
Binary file not shown.

mkdocs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ nav:
253253
- "2025-W09-02": summary/2025/2025-W09-02.md
254254
- "2025-W11-03": summary/2025/2025-W11-03.md
255255
- "2025-W12-03": summary/2025/2025-W12-03.md
256+
- "2025-W16-04": summary/2025/2025-W16-04.md
256257
- Tools:
257258
- Tools/index.md
258259
- Cheat Sheet:
@@ -309,7 +310,7 @@ nav:
309310
- Tools/Environment/obsidian_setup.md
310311
- Make:
311312
- Tools/Make/CMake.md
312-
- Tools/Make/Makeflie.md
313+
- Tools/Make/Makeflie.md
313314
- Others:
314315
- Tools/Others/Chezmoi.md
315316
- Tools/Others/SSH.md

0 commit comments

Comments
 (0)