Python 保存在数据帧中检测到的对象:tensorflow对象检测
我正在运行github存储库tensorflow/object_deteciton中的典型代码: 特别是“object\u detection\u tutorial.ipynb”文件。主回路在此部分中:Python 保存在数据帧中检测到的对象:tensorflow对象检测,python,tensorflow,object-detection,Python,Tensorflow,Object Detection,我正在运行github存储库tensorflow/object_deteciton中的典型代码: 特别是“object\u detection\u tutorial.ipynb”文件。主回路在此部分中: with detection_graph.as_default(): with tf.Session(graph=detection_graph) as sess: # Definite input and output Tensors for detection_graph
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
我只是在寻找一些建议,告诉你如何将图像识别的内容保存到一个数据框中,这个数据框可以理想地存储图像中检测到的每个对象的对象类别
任何帮助都将不胜感激(:好吧,我想已经很晚了,但我现在正在做这件事。所以我在几天的时间里经历了同样的痛苦,最后得到了一些工作。我只限于您的代码片段,添加了一些片段,得到了以下结果:
# Initialize hitlist
hitf = open("hitlist.csv",'w')
hitf.write('image,class,score,bb0,bb1,bb2,bb3\n')
hitlim = 0.5
with detection_graph.as_default():
with tf.Session(graph=detection_graph) as sess:
# Definite input and output Tensors for detection_graph
image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
# Each box represents a part of the image where a particular object was detected.
detection_boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
# Each score represent how level of confidence for each of the objects.
# Score is shown on the result image, together with the class label.
detection_scores = detection_graph.get_tensor_by_name('detection_scores:0')
detection_classes = detection_graph.get_tensor_by_name('detection_classes:0')
num_detections = detection_graph.get_tensor_by_name('num_detections:0')
for image_path in TEST_IMAGE_PATHS:
image = Image.open(image_path)
# the array based representation of the image will be used later in order to prepare the
# result image with boxes and labels on it.
image_np = load_image_into_numpy_array(image)
# Expand dimensions since the model expects images to have shape: [1, None, None, 3]
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
(boxes, scores, classes, num) = sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_np_expanded})
# Write the results to hitlist - one line per hit over the 0.5
nprehit = scores.shape[1] # 2nd array dimension
for j in range(nprehit):
fname = "image"+str(i)
classid = int(classes[i][j])
classname = category_index[classid]["name"]
score = scores[i][j]
if (score>=hitlim):
sscore = str(score)
bbox = boxes[i][j]
b0 = str(bbox[0])
b1 = str(bbox[1])
b2 = str(bbox[2])
b3 = str(bbox[3])
line = ",".join([fname,classname,sscore,b0,b1,b2,b3])
hitf.write(line+"\n")
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=8)
plt.figure(figsize=IMAGE_SIZE)
plt.imshow(image_np)
# close hitlist
hitf.flush()
hitf.close()
注:
- 添加的代码有三个部分,一个初始化
,一个在置信限0.5以上的“预命中”中添加一行,一个关闭文件hitlist.csv
- 它故意不太“pythonic”,它使用简单而明显的结构来说明正在发生的事情。除了
,我非常喜欢它,我无法抗拒“,”.join(…)
- 通过查看分数的第二维度或ClassID,可以找到预命中数
- 返回的classid是
s,尽管您通常需要将它们作为整数。转换很容易float
- 这里可能有一些小的复制和粘贴错误,因为我没有一个真正的MVE(最小可验证示例)
- 我使用的是
目标检测模型,而不是rfcn\u resnet101\u coco\u 2017\u 11\u 08
,所以我的命中列表和分数有点不同(实际上更糟)ssd\u mobilenet\u v1\u coco\u 2017\u 11\u 17
image,class,score,bb0,bb1,bb2,bb3
image0,kite,0.997912,0.086756825,0.43700624,0.1691603,0.4966739
image0,person,0.9968072,0.7714941,0.15771112,0.945292,0.20014654
image0,person,0.9858992,0.67766637,0.08734644,0.8385928,0.12563995
image0,kite,0.9683157,0.26249793,0.20640253,0.31359094,0.2257214
image0,kite,0.8578382,0.3803091,0.42938906,0.40701985,0.4453904
image0,person,0.85244817,0.5692219,0.06266626,0.6282138,0.0788657
image0,kite,0.7622662,0.38192448,0.42580333,0.4104231,0.442965
image0,person,0.6722884,0.578461,0.022049228,0.6197509,0.036917627
image0,kite,0.6671517,0.43708095,0.80048573,0.47312954,0.8156846
image0,person,0.6577289,0.5996533,0.13272598,0.63358027,0.1430584
image0,kite,0.5893124,0.3790631,0.3451705,0.39845183,0.35965574
image0,person,0.51051,0.57377476,0.025907507,0.6221084,0.04294989
对于此图像(来自ipython笔记本-但具有不同的对象检测模型)
我认为所描述的object\u detection.py文件的语法发生了一些变化。我对所描述的答案进行了一些调整,以适应新的语法: 这是您应该在代码中找到的位置:
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
然后可以添加以下内容:
# store boxes in dataframe!
cut_off_scores = len(list(filter(lambda x: x >= 0.1, output_dict['detection_scores'])))
detect_scores = []
detect_classes = []
detect_ymin = []
detect_xmin = []
detect_ymax = []
detect_xmax = []
for j in range(cut_off_scores):
detect_scores.append(output_dict['detection_scores'][j])
detect_classes.append(output_dict['detection_classes'][j])
# Assumption: ymin, xmin, ymax, xmax:
boxes = output_dict['detection_boxes'][j]
detect_ymin.append(boxes[0])
detect_xmin.append(boxes[1])
detect_ymax.append(boxes[2])
detect_xmax.append(boxes[3])
# Assumption: your files are named image1, image2, etc.
Identifier = ("image" + str(n))
Id_list = [Identifier] * cut_off_scores
Detected_objects = pd.DataFrame(
{'Image': Id_list,
'Score': detect_scores,
'Class': detect_classes,
'Ymin': detect_ymin,
'Xmax': detect_xmax,
'Ymax': detect_ymax,
'Xmax': detect_xmax
})
我尝试了以上两种方法,但都没有成功。 和User27074,在附加xmax、xmin等值时出现问题 我尝试了一个简单的代码,它的工作只是检测到的物体的坐标保存在百分比,需要乘以高度和宽度的图像后
detected_boxes = []
h = image_height = 500 #Change accordingly
w = image_width = 500 #change accordingly
#Columns' format 'ymin','xmin','ymax', 'xmax', 'class', 'Detection score'
for i, box in enumerate(np.squeeze(boxes)):
if (np.squeeze(scores)[i] > 0.85):
box[0] = int(box[0] * h)
box[1] = int(box[1] * w)
box[2] = int(box[2] * h)
box[3] = int(box[3] * w)
box = np.append(box, np.squeeze(classes)[i])
box = np.append(box, np.squeeze(scores)[i]*100)
detected_boxes.append(box)
np.savetxt('detection_coordinates.csv', detected_boxes, fmt='%i', delimiter=',')
我可以说,你有太多的评论。好的评论是稀疏的,因为好的代码是自我记录的。任何时候你想表达为什么你选择了一个不明显的特定算法或技术,你应该留下一条评论,讨论为什么代码是这样写的。任何时候你有一段代码,尽管名字很好iers,没有明确的目的。您可以留下注释来记录代码的功能。不要留下包含通过阅读代码可以了解的信息的注释。变量名,可理解的操作,等等。我不确定您是否知道,但这段代码取自Github的教程,这就是为什么它有c我想这里面有什么内容……有什么反馈吗?我想新TF版本中的代码随着时间的推移已经发生了变化,但是您显示的这段代码不在您描述的文件中。@user27074那么答案是否仍然有用呢?我会在有机会的时候看一看。