在這段代碼中,我正在檢測奶牛的乳房部分,它在乳房周圍顯示了一個邊界框,還顯示了準確性和檢測等級,但影像結果大小非常小,我看不到結果。
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
category_index = label_map_util.create_category_index_from_labelmap(files['LABELMAP'])
img = cv2.imread(IMAGE_PATH)
image_np = np.array(img)
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
detections = detect_fn(input_tensor)
num_detections = int(detections.pop('num_detections'))
detections = {key: value[0, :num_detections].numpy()
for key, value in detections.items()}
detections['num_detections'] = num_detections
# detection_classes should be ints.
detections['detection_classes'] = detections['detection_classes'].astype(np.int64)
label_id_offset = 1
image_np_with_detections = image_np.copy()
viz_utils.visualize_boxes_and_labels_on_image_array(
image_np_with_detections,
detections['detection_boxes'],
detections['detection_classes'] label_id_offset,
detections['detection_scores'],
category_index,
use_normalized_coordinates=True,
max_boxes_to_draw=5,
min_score_thresh=.8,
agnostic_mode=False)
plt.imshow(cv2.cvtColor(image_np_with_detections, cv2.COLOR_BGR2RGB))
plt.show()
在這張圖片中,我正在檢測奶牛的乳房部分
uj5u.com熱心網友回復:
你需要使用 matplotlib 嗎?使用枕頭和情節,您可以突出顯示感興趣的區域,如下所示。您可以互動放大并顯示精度,例如懸停在邊界框上。
import PIL.Image
import plotly.express as px
import plotly.graph_objects as go
img = PIL.Image.open('l3nQn.png')
# Define bbox for drawing
left = 105
top = 81
height = 72
width = 35
# Define accuracy for hover text
accuracy = .98
# Draw image
fig = px.imshow(img)
fig.update_xaxes(visible=False, range=[0, img.size[0]])
# Draw bounding box in separate trace
bbox_trace = go.Scatter(
x=(left width, left, left, left width, left width),
y=(top, top, top height, top height, top),
fill='toself',
name=f'acc={accuracy}',
)
fig.add_trace(bbox_trace)
fig.show()
與 plotly 的邊界框。將滑鼠懸停在它上面會顯示準確度
uj5u.com熱心網友回復:
發生這種情況是因為您的原始影像非常大。如果您真的想調整影像大小,您應該在代碼的開頭進行,然后再使用檢測模型。您可以使用 cv2.resize。例如將原始影像大小除以 4 :
resized = cv2.resize(img, (width//4, height//4), interpolation = cv2.INTER_LINEAR)
但是您可能更喜歡使用更大的文本而不是調整影像本身的大小。在這種情況下,也許可以試試這個,只使用 matplotlib 和 cv2 而不是物件檢測中的“viz_utils”。您可以在 cv2.putText 的選項中更改文本的大小、偏移量、顏色(等)。
import cv2
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
def detect_and_classify_udder(file):
image = cv2.imread(file, 3)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
input_tensor = tf.convert_to_tensor(image)
# input_tensor = input_tensor[tf.newaxis,...] # if needed
detections_dict = detect_fn(input_tensor) # detect_fn is your detection model
num_detections = int(detections_dict.pop('num_detections'))
detections_dict = {key:value[0, :num_detections].numpy() for key,value in detections_dict.items()}
scores = np.array(detections_dict['detection_scores'])
boxes = detections_dict['detection_boxes']
# add a threshold under which the box is not drawn
real_boxes = boxes[np.where(scores > 0.8)]
real_scores = scores[np.where(scores > 0.8)]
real_boxes_scaled =[]
for box in real_boxes :
x1 = int(image.shape[1]*box[1])
x2 = int(image.shape[1]*box[3])
y1 = int(image.shape[0]*box[0])
y2 = int(image.shape[0]*box[2])
real_boxes_scaled.append([x1, x2, y1, y2])
for box, score in zip(real_boxes_scaled, real_scores) :
x1, x2, y1, y2 = box
# draw the rectangle with the coordinates
image = cv2.rectangle(image, (x1, y1), (x2, y2), (255, 255, 0), 5)
# add caption with putText
image = cv2.putText(image, score, (x1-22, y1-16), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255,255,0), 2)
plt.figure(figsize=(8, 8))
plt.axis("off")
plt.imshow(image)
putText 用法:
cv2.putText(image, text, origin, fontType, fontScale, Color, Thickness)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/398282.html
