Skip to content

Commit 4e1de8f

Browse files
committed
update 目标检测结果叠加到图片中
1 parent 72ba81b commit 4e1de8f

2 files changed

Lines changed: 11 additions & 52 deletions

File tree

opencv_tools/jade_opencv_process.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -390,50 +390,6 @@ def _to_color(indx):
390390
return b * 255, r * 255, g * 255
391391

392392

393-
# opencv显示boxes
394-
def CVShowBoxes(image, detectresult, num_classes=90, waitkey=-1, named_windows="result"):
395-
base = int(np.ceil(pow(num_classes, 1. / 3)))
396-
colors = [_to_color(x) for x in range(num_classes)]
397-
if type(image) == str:
398-
image = cv2.imread(image)
399-
image2 = image.copy()
400-
boxes = detectresult.boxes
401-
for i in range(len(boxes)):
402-
if boxes[i][0] <= 1 and boxes[i][1] <= 1 and boxes[i][2] <= 1 and boxes[i][3] <= 1:
403-
xmin = int(boxes[i][0] * image.shape[1])
404-
ymin = int(boxes[i][1] * image.shape[0])
405-
xmax = int(boxes[i][2] * image.shape[1])
406-
ymax = int(boxes[i][3] * image.shape[0])
407-
else:
408-
xmin = int(boxes[i][0])
409-
ymin = int(boxes[i][1])
410-
xmax = int(boxes[i][2])
411-
ymax = int(boxes[i][3])
412-
if boxes is not None:
413-
image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), GetRandomColor(), 3, 3)
414-
if detectresult.label_texts is not None:
415-
if detectresult.scores is not None:
416-
image2 = Add_Chinese_Label(img=image2, label=detectresult.label_texts[i] + ":" + str(
417-
int(detectresult.scores[i] * 100)),
418-
pt1=(xmin, ymin))
419-
else:
420-
image2 = Add_Chinese_Label(img=image2, label=detectresult.label_texts[i],
421-
pt1=(xmin, ymin))
422-
if detectresult.label_ids is not None:
423-
image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), colors[int(detectresult.label_ids[i])],
424-
3, 3)
425-
else:
426-
image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), GetRandomColor(), 3, 3)
427-
428-
if waitkey >= 0:
429-
cv2.namedWindow(named_windows, 0)
430-
# cv2.resizeWindow("result", 840, 680)
431-
cv2.imshow(named_windows, image2)
432-
cv2.waitKey(waitkey)
433-
else:
434-
return image2
435-
436-
437393
# opencv显示points
438394
def CVShowPoints(img_path, points, waitkey=1):
439395
if type(img_path) != list:
@@ -916,5 +872,5 @@ def run(self):
916872
if __name__ == '__main__':
917873
from jade import JadeLogging
918874
JadeLog = JadeLogging("log",Level="DEBUG")
919-
videoCaptureThread = VideoCaptureBaseProcess("rtsp://admin:samples123@192.168.29.181:554/h264/ch1/main/av_stream","top",True,30,JadeLog)
875+
videoCaptureThread = VideoCaptureBaseProcess("rtsp://admin:samples123@192.168.29.181:554/h264/ch1/main/av_stream","top",False,30,JadeLog)
920876
videoCaptureThread.start()

opencv_tools/jade_visualize.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_color_map_list(num_classes):
3333
color_map = [color_map[i:i + 3] for i in range(0, len(color_map), 3)]
3434
return color_map
3535

36-
def draw_box(im, results,show_score=True):
36+
def draw_box(im, results,show_score=True,font_path=None,font_size=24):
3737
"""
3838
Args:
3939
im (PIL.Image.Image): PIL image
@@ -69,10 +69,11 @@ def draw_box(im, results,show_score=True):
6969
text = "{} {:.4f}".format(labels_text[i,], scores[i,])
7070
else:
7171
text = "{} ".format(labels_text[i,])
72-
tw, th = draw.textsize(text)
72+
font = ImageFont.truetype(get_font_path(font_path), font_size, encoding="utf-8") # 参数1:字体文件路径,参数2:字体大小
73+
tw, th = draw.textsize(text,font)
7374
draw.rectangle(
7475
[(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color)
75-
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255))
76+
draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255),font=font)
7677
return im
7778

7879

@@ -207,7 +208,7 @@ def draw_mask(im, np_boxes, np_masks, labels, resolution=14, threshold=0.5):
207208
return Image.fromarray(im.astype('uint8'))
208209

209210

210-
def visualize_box_mask(im, results, mask_resolution=14,show_score=True):
211+
def visualize_box_mask(im, results, mask_resolution=14,show_score=True,font_path=None,font_size=12):
211212
"""
212213
Args:
213214
im (str/np.ndarray): path of image/np.ndarray read by cv2
@@ -233,7 +234,7 @@ def visualize_box_mask(im, results, mask_resolution=14,show_score=True):
233234
results["labels"],
234235
resolution=mask_resolution)
235236
if 'boxes' in results:
236-
im = draw_box(im, results,show_score)
237+
im = draw_box(im, results,show_score,font_path,font_size)
237238
if 'segm' in results:
238239
im = draw_segm(
239240
im,
@@ -248,13 +249,15 @@ def visualize_box_mask(im, results, mask_resolution=14,show_score=True):
248249
def visualize(image_file,
249250
results,
250251
mask_resolution=14,
251-
show_score=True):
252+
show_score=True,font_path=None,font_size=12):
252253
# visualize the predict result
253254
im = visualize_box_mask(
254255
image_file,
255256
results,
256257
mask_resolution=mask_resolution,
257-
show_score=show_score)
258+
show_score=show_score,
259+
font_path=font_path,
260+
font_size=font_size)
258261
return np.array(im)
259262

260263

0 commit comments

Comments
 (0)