简体   繁体   English

如何在for循环中只打印一次语句

[英]How to print statement only once inside a for loop

Here is a part my real time object detection code .(full script : https://github.com/aswinr22/waste-model/blob/master/picamera1.py ) 这是我的实时对象检测代码的一部分。(完整脚本: 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")

my output is this: 我的输出是这样的:

e waste detected
e waste detected
e waste detected
e waste detected..
.....
.... and so on.

what i want is to print this statement only once. 我想要的只是打印一次这个声明。 what can i do please help me 我该怎么办请帮助我

You can use the break statement to get out of your for loop if your condition is triggered. 如果您的条件被触发,您可以使用break语句退出for循环。

EDIT: without having the data file it's hard to make this work exactly with the code you have on github, but here's a toy example that's similar to your use case: 编辑:没有数据文件很难使这个工作完全与您在github上的代码,但这是一个类似于您的用例的玩具示例:

classes= [0,2,2,1,2]

for item in (classes): # here is my classes id is retrieved
    if(item == 2):
        print("e waste detected")
        break
print("post-loop")

Removing the break, you'll see the behavior you're seeing now - but note the indentation, it should be inside of the if statement. 删除中断,您将看到您现在看到的行为 - 但请注意缩进,它应该在if语句中。

Try this (code added is marked with # NEW comment) 试试这个(添加的代码标有#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
wasted = (c==2 and s>0.5 for c, s  in zip(classes, scores))
if any(wasted):
  print("wasted detected")

Double braces mean generator comprehension, it stops when any founds first true value. 双括号意味着生成器理解,当any创建的第一个真值时它停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM