如何解决如何在For中打印某些内容而无需重复?
我有以下问题:我在项目中使用Yolo来检测视频上的对象,当代码检测到对象时,我需要警告,但是代码发送了很多对象,而我只需要一次,我了解到您可以设置一个条件,以便不会重复打印,但是我不知道该怎么做,这是代码的一部分:
for detection in detections:
if detection is not None:
detection = rescale_boxes(detection,opt.img_size,RGBimg.shape[:2])
for x1,y1,x2,y2,conf,cls_conf,cls_pred in detection:
box_w = x2 - x1
box_h = y2 - y1
color = [int(c) for c in colors[int(cls_pred)]]
print("there is {}".format(classes[int(cls_pred)]))
frame = cv2.rectangle(frame,(x1,y1 + box_h),(x2,y1),color,5)
cv2.putText(frame,classes[int(cls_pred)],cv2.FONT_HERSHEY_SIMPLEX,1,5)
cv2.putText(frame,str("%.2f" % float(conf)),y2 - box_h),0.5,5)
我通过以下方法解决了这个问题:
x=0
def add():
global x
x=x+1
我不知道这是否是解决问题的最佳方法,但它确实有效。
for detection in detections:
if detection is not None:
detection = rescale_boxes(detection,RGBimg.shape[:2])
for x1,cls_pred in detection:
if x<=0:
add()
print("Se detectó posible {}".format(classes[int(cls_pred)]))
box_w = x2 - x1
box_h = y2 - y1
color = [int(c) for c in colors[int(cls_pred)]]
frame = cv2.rectangle(frame,5)
cv2.putText(frame,5)# Nombre de la clase detectada
cv2.putText(frame,5) # Certeza de prediccion de la clase
解决方法
如果您只想在第一次迭代中做某事(例如打印),则可以设置一个标志:
first = True
for .........
# somewhere inside your loop
if first:
# print or whatever
first = False
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。