|
13 | 13 | from PIL import Image, ImageFont, ImageDraw |
14 | 14 | from opencv_tools.jade_opencv_process import GetRandomColor,ReadChinesePath |
15 | 15 | from jade import getOperationSystem |
| 16 | +import random |
| 17 | + |
| 18 | +def _to_color(indx): |
| 19 | + """ return (b, r, g) tuple""" |
| 20 | + b = random.randint(1,10) / 10 |
| 21 | + g = random.randint(1,10) / 10 |
| 22 | + r = random.randint(1,10) / 10 |
| 23 | + return b * 255, r * 255, g * 255 |
| 24 | + |
16 | 25 | def get_color_map_list(num_classes): |
17 | 26 | """ |
18 | 27 | Args: |
@@ -43,6 +52,8 @@ def draw_box(im, results,show_score=True,font_path=None,font_size=24): |
43 | 52 | Returns: |
44 | 53 | im (PIL.Image.Image): visualized image |
45 | 54 | """ |
| 55 | + |
| 56 | + im = Image.fromarray(im) |
46 | 57 | np_boxes = results['boxes'] |
47 | 58 | labels_text = results["labels"] |
48 | 59 | scores = results["scores"] |
@@ -74,6 +85,7 @@ def draw_box(im, results,show_score=True,font_path=None,font_size=24): |
74 | 85 | draw.rectangle( |
75 | 86 | [(xmin + 1, ymin - th), (xmin + tw + 1, ymin)], fill=color) |
76 | 87 | draw.text((xmin + 1, ymin - th), text, fill=(255, 255, 255),font=font) |
| 88 | + im = np.array(im) |
77 | 89 | return im |
78 | 90 |
|
79 | 91 |
|
@@ -603,6 +615,48 @@ def CVShowKeypointsBoxes(img_path, keypoints, bboxes=[], scores=[], waitkey=1): |
603 | 615 | cv2.imshow("result", im) |
604 | 616 | cv2.waitKey(waitkey) |
605 | 617 |
|
| 618 | + |
| 619 | +#opencv显示boxes |
| 620 | +def CVShowBoxes(image,boxes,label_texts,scores,label_ids=None,num_classes=90,waitkey=-1,named_windows="result"): |
| 621 | + base = int(np.ceil(pow(num_classes, 1. / 3))) |
| 622 | + colors = [_to_color(x) for x in range(num_classes)] |
| 623 | + if type(image) == str: |
| 624 | + image = cv2.imread(image) |
| 625 | + image2 = image.copy() |
| 626 | + for i in range(len(boxes)): |
| 627 | + if boxes[i][0] <= 1.1 and boxes[i][1] <= 1.1 and boxes[i][2] <= 1.1 and boxes[i][3] <= 1.1: |
| 628 | + xmin = int(boxes[i][0]*image.shape[1]) if int(boxes[i][0]*image.shape[1]) > 0 else 0 |
| 629 | + ymin = int(boxes[i][1]*image.shape[0]) if (int(boxes[i][1]*image.shape[0])) > 0 else 0 |
| 630 | + xmax = int(boxes[i][2]*image.shape[1]) if int(boxes[i][2]*image.shape[1]) > 0 else 0 |
| 631 | + ymax = int(boxes[i][3]*image.shape[0]) if int(boxes[i][3]*image.shape[0]) > 0 else 0 |
| 632 | + else: |
| 633 | + xmin = int(boxes[i][0]) |
| 634 | + ymin = int(boxes[i][1]) |
| 635 | + xmax = int(boxes[i][2]) |
| 636 | + ymax = int(boxes[i][3]) |
| 637 | + if boxes is not None: |
| 638 | + image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), GetRandomColor(), 3, 3) |
| 639 | + if label_texts is not None: |
| 640 | + if scores is not None: |
| 641 | + image2 = Add_Chinese_Label(img=image2, label=label_texts[i] + ":" + str(int(scores[i] * 100)), |
| 642 | + pt1=(xmin, ymin)) |
| 643 | + else: |
| 644 | + image2 = Add_Chinese_Label(img=image2, label=label_texts[i], |
| 645 | + pt1=(xmin, ymin)) |
| 646 | + if label_ids is not None: |
| 647 | + image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), colors[int(label_ids[i])], 3, 3) |
| 648 | + else: |
| 649 | + image2 = cv2.rectangle(image2, (xmin, ymin), (xmax, ymax), GetRandomColor(), 3, 3) |
| 650 | + |
| 651 | + |
| 652 | + if waitkey >= 0: |
| 653 | + cv2.namedWindow(named_windows, 0) |
| 654 | + # cv2.resizeWindow("result", 840, 680) |
| 655 | + cv2.imshow(named_windows,image2) |
| 656 | + cv2.waitKey(waitkey) |
| 657 | + else: |
| 658 | + return image2 |
| 659 | + |
606 | 660 | # OCR识别结果 |
607 | 661 | def draw_ocr(image, boxes, txts, scores, draw_txt=True, drop_score=0.5): |
608 | 662 | """ |
|
0 commit comments