python - 如何在for循环中只打印一次语句
问题描述
这是我的实时对象检测代码的一部分。(完整脚本:https ://github.com/aswinr22/waste-model/blob/master/picamera1.py )
for i in range (classes.size): # here is my classes id is retrieved
if(classes[0][i] == 2 and scores[0][i]>0.5):
print("e waste detected")
我的输出是这样的:
e waste detected
e waste detected
e waste detected
e waste detected..
.....
.... and so on.
我想要的是只打印一次这个语句。我能做什么请帮帮我
解决方案
试试这个(添加的代码标有 # NEW 注释)
...
waste_found = False # NEW
for frame1 in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
t1 = cv2.getTickCount()
# Acquire frame and expand frame dimensions to have shape: [1, None, None, 3]
# i.e. a single-column array, where each item in the column has the pixel RGB value
frame = np.copy(frame1.array)
frame.setflags(write=1)
frame_expanded = np.expand_dims(frame, axis=0)
# Perform the actual detection by running the model with the image as input
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: frame_expanded})
# Draw the results of the detection (aka 'visulaize the results')
vis_util.visualize_boxes_and_labels_on_image_array(
frame,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8,
min_score_thresh=0.40)
# p = GPIO.PWM(servoPIN, 50)
# p.start(2.5)
for i in range(classes.size):
if (classes[0][i] == 2 and scores[0][i] > 0.5):
print("e waste detected")
waste_found = True # NEW
break # NEW
# elif(classes[0][i] == 1 and scores[0][i]>0.5):
# print("recycle detected")
# p.start(2.5) # Initialization
## p.ChangeDutyCycle(5)
# time.sleep(4)
# p.ChangeDutyCycle(10)
# time.sleep(4)
# except KeyboardInterrupt:
# p.stop()
# GPIO.cleanup()
if waste_found: # NEW
break # NEW
# return image_np
推荐阅读
- xml - 我想使用 XMLT [1.0] 根据标签值命名我的输出 xml 文件吗?
- javascript - Vue 可以在新网站上使用 Paypal 沙盒帐户进行真实交易
- java - 识别 Camunda 中的子执行
- reactjs - 如何使用 Jest & Enzyme (useField Hook) 为 Formik 驱动的输入组件编写测试?
- r - 如何以 10000 次重复优化 r 中的分层随机抽样
- python - python在写入文件时在行之间创建空行
- javascript - 为什么 console.log 的行为与 .innerText/.innerHTML 属性不同
- opencv - 如何使用 cv2.grabcut 改进前景提取
- javascript - 使用 mongodb 在 nodejs 上对用户的参与者进行分组
- angularjs - 接受指定状态 URL 之后的任何后续字符