这是我的实时对象检测代码的一部分。(完整脚本:https://github.com/aswinr22/waste-model/blob/master/picamera1.py)
import pandas as pd
#Existing DF where the data is in the form of list
df = pd.DataFrame(columns=['ID', 'value_list'])
#New DF where the data should be atomic
df_new = pd.DataFrame(columns=['ID', 'value_single'])
#Sample Data
row_1 = ['A', 'B', 'C', 'D']
row_2 = ['D', 'E', 'F']
row_3 = ['F', 'G']
row_4 = ['H', 'I']
row_5 = ['J']
#Data Push to existing DF
row_ = "row_"
for i in range(5):
df.loc[i, 'ID'] = i
df.loc[i, 'value_list'] = eval(row_+str(i+1))
#Data Push to new DF where list is pushed as atomic data
counter = 0
i=0
while(i<len(df)):
j=0
while(j<len(df['value_list'][i])):
df_new.loc[counter, 'ID'] = df['ID'][i]
df_new.loc[counter, 'value_single'] = df['value_list'][i][j]
counter = counter + 1
j = j+1
i = i+1
print(df_new)
我的输出是这样:
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")
我要打印的语句仅一次。我该怎么办请帮助我
答案 0 :(得分:0)
如果触发了条件,则可以使用break statement退出for循环。
编辑:在没有数据文件的情况下,很难与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")
删除中断,您将看到现在看到的行为-但请注意缩进,它应位于if语句的内部。
答案 1 :(得分:0)
尝试一下(添加的代码带有#条新注释)
...
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
答案 2 :(得分:0)
wasted = (c==2 and s>0.5 for c, s in zip(classes, scores))
if any(wasted):
print("wasted detected")
双括号表示生成器理解,它在any
发现第一个真值时停止。