14 Commits e66a17b2f8 ... f589d35aec

Author SHA1 Message Date
  Raychar f589d35aec Merge branch 'master' of http://gogsb.soaringnova.com/chenguilong/yl-ocr-layout 2 years ago
  Raychar e0b8f53431 xxc 交接 2 years ago
  Raychar f9e641d1b0 first 2 years ago
  Raychar 69e8ec78ce first 2 years ago
  Raychar ba9ddf64fc first 2 years ago
  Raychar d6adbd797c first commit 2 years ago
  Raychar 60d3b38ad6 first commit 2 years ago
  zeke-chin 3e4238836d 更新模型使用 2 years ago
  zeke-chin b79d9325ce Revert "更新: 版面检测模型" 2 years ago
  meishuhuan 309713a1cc Merge branch 'master' of https://gogsb.soaringnova.com/chenguilong/yl-ocr-layout 2 years ago
  zeke-chin c601e36473 更新: 版面检测模型 2 years ago
  zeke-chin c122502c4b Merge branch 'master' of https://gogs.soaringnova.com/sxwl_DL/yl-ocr-layout 2 years ago
  zeke-chin 5059ee4b67 更新: 版面检测模型 2 years ago
  sxwl_DL ce07b32b5d 更新 'Dockerfile' 2 years ago

BIN
.DS_Store


+ 0 - 2
.gitattributes

@@ -1,2 +0,0 @@
-# Auto detect text files and perform LF normalization
-* text=auto

+ 0 - 6
.gitignore

@@ -1,6 +0,0 @@
-/.vscode/
-*.pt
-/.idea/
-
-__pycache__/*
-*/__pycache__/*

+ 8 - 0
.idea/.gitignore

@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml

+ 22 - 0
.idea/deployment.xml

@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="PublishConfigData" autoUpload="Always" exclude=".svn;.cvs;.idea;.DS_Store;.git;.hg;*.hprof;*.pyc;test_imgs" remoteFilesAllowedToDisappearOnAutoupload="false">
+    <serverData>
+      <paths name="root@192.168.199.249:4868">
+        <serverdata>
+          <mappings>
+            <mapping deploy="/workspace" local="$PROJECT_DIR$" />
+          </mappings>
+        </serverdata>
+      </paths>
+      <paths name="root@192.168.199.249:4868 (2)">
+        <serverdata>
+          <mappings>
+            <mapping deploy="/workspace" local="$PROJECT_DIR$" />
+          </mappings>
+        </serverdata>
+      </paths>
+    </serverData>
+    <option name="myAutoUpload" value="ALWAYS" />
+  </component>
+</project>

+ 6 - 0
.idea/inspectionProfiles/profiles_settings.xml

@@ -0,0 +1,6 @@
+<component name="InspectionProjectProfileManager">
+  <settings>
+    <option name="USE_PROJECT_PROFILE" value="false" />
+    <version value="1.0" />
+  </settings>
+</component>

+ 4 - 0
.idea/misc.xml

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (py38)" project-jdk-type="Python SDK" />
+</project>

+ 8 - 0
.idea/modules.xml

@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="ProjectModuleManager">
+    <modules>
+      <module fileurl="file://$PROJECT_DIR$/.idea/yl-ocr-layout.iml" filepath="$PROJECT_DIR$/.idea/yl-ocr-layout.iml" />
+    </modules>
+  </component>
+</project>

+ 6 - 0
.idea/vcs.xml

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="VcsDirectoryMappings">
+    <mapping directory="$PROJECT_DIR$" vcs="Git" />
+  </component>
+</project>

+ 20 - 0
.idea/yl-ocr-layout.iml

@@ -0,0 +1,20 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="PYTHON_MODULE" version="4">
+  <component name="NewModuleRootManager">
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="jdk" jdkName="Python 3.8 (py38)" jdkType="Python SDK" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+  <component name="PyDocumentationSettings">
+    <option name="format" value="PLAIN" />
+    <option name="myDocStringFormat" value="Plain" />
+  </component>
+  <component name="TemplatesService">
+    <option name="TEMPLATE_CONFIGURATION" value="Jinja2" />
+    <option name="TEMPLATE_FOLDERS">
+      <list>
+        <option value="$MODULE_DIR$/templates" />
+      </list>
+    </option>
+  </component>
+</module>

+ 1 - 1
Dockerfile

@@ -93,7 +93,7 @@ startretries=0\n\
 redirect_stderr=true\n\
 redirect_stderr=true\n\
 stdout_logfile=/var/log/be.log\n\
 stdout_logfile=/var/log/be.log\n\
 stdout_logfile_maxbytes=50MB\n\
 stdout_logfile_maxbytes=50MB\n\
-environment=CUDA_VISIBLE_DEVICES=0,PYTHONUNBUFFERED=1,PYTHONPATH=/worksapce/yolov5\n\
+environment=CUDA_VISIBLE_DEVICES=0,PYTHONPATH=/worksapce/yolov5\n\
 " > /etc/supervisor/conf.d/be.conf
 " > /etc/supervisor/conf.d/be.conf
 
 
 # envs/py38/
 # envs/py38/

+ 2 - 2
client.py

@@ -13,8 +13,8 @@ from io import BytesIO
 from PIL import Image
 from PIL import Image
 
 
 def send_request(file_list = ['./images/zidane.jpg'], 
 def send_request(file_list = ['./images/zidane.jpg'], 
-                    model_name = 'yolov5x',
-                    img_size = 800,
+                    model_name = 'yolov5s',
+                    img_size = 640,
                     download_image = False):
                     download_image = False):
 
 
     #upload multiple files as list of tuples
     #upload multiple files as list of tuples

+ 182 - 0
create_md.py

@@ -0,0 +1,182 @@
+"""
+function: 生成md文件
+"""
+from mdutils.mdutils import MdUtils
+import os
+import cv2
+import numpy as np
+import requests
+
+d_map = {0: 'code', 1: 'logo', 2: 'style', 3: 'table', 4: 'text', 5: 'title'}
+images_path = "./train/images/"
+labels_path = "./train/labels/"
+cut_ori_img_path = "./train/cut_ori_imgs/"
+cut_predict_img_path = "./train/cut_predict_imgs/"
+md_cut_ori_img_path = "../train/cut_ori_imgs/"
+md_cut_predict_img_path = "../train/cut_predict_imgs/"
+md_path = "mdfiles/"
+
+
+# 标签是经过归一化的,需要变回来
+# xywh格式 ---> box四个顶点的坐标
+def xywh2lrbt(img_w, img_h, box):
+    c, x, y, w, h = int(box[0]), float(box[1]), float(box[2]), float(box[3]), float(box[4])
+    x = x * img_w  # 中心坐标x
+    w = w * img_w  # box的宽
+    y = y * img_h  # 中心坐标y
+    h = h * img_h  # box的高
+
+    lt_x, lt_y = x - w / 2, y - h / 2  # left_top_x, left_top_y
+    lb_x, lb_y = x - w / 2, y + h / 2  # left_bottom_x, left_bottom_y
+    rb_x, rb_y = x + w / 2, y + h / 2  # right_bottom_x, right_bottom_y
+    rt_x, rt_y = x + w / 2, y - h / 2  # right_top_x, right_bottom_y
+
+    lrbt = [[lt_x, lt_y], [lb_x, lb_y], [rb_x, rb_y], [rt_x, rt_y]]
+
+    return lrbt, c
+
+
+def IOU(rect1, rect2):
+    xmin1, ymin1, xmax1, ymax1 = rect1
+    xmin2, ymin2, xmax2, ymax2 = rect2
+    s1 = (xmax1 - xmin1) * (ymax1 - ymin1)
+    s2 = (xmax2 - xmin2) * (ymax2 - ymin2)
+
+    sum_area = s1 + s2
+
+    left = max(xmin2, xmin1)
+    right = min(xmax2, xmax1)
+    top = max(ymin2, ymin1)
+    bottom = min(ymax2, ymax1)
+
+    if left >= right or top >= bottom:
+        return 0
+
+    intersection = (right - left) * (bottom - top)
+    return intersection / (sum_area - intersection) * 1.0
+
+
+def send_requests(img_path):
+    b_img_list = []
+    b_img_list.append(('file_list', ('image.jpeg', open(img_path, 'rb'), 'image/jpeg')))
+    payload = {'model_name': 'ocr-layout', 'img_size': 1920, 'download_image': False}
+    response = requests.request("POST", "http://192.168.199.249:4869/detect", headers={}, data=payload,
+                                files=b_img_list)
+    result = eval(str(response.text))
+    result = eval(str(result['result']))
+    return result
+
+
+if __name__ == "__main__":
+    list_images = os.listdir(images_path)
+    list_labels = os.listdir(labels_path)
+    all_acc = 0
+    all_label_len = 0
+
+    # 遍历每张图片
+    for m in range(len(list_images)):
+        each_acc_map = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0, 5: 0}  # 每张图片对应的各自预测正确数量
+        each_acc = 0  # 每张图片总共预测正确的数量
+
+        image = list_images[m]
+        img_name = image.split(".")[0]
+        # if image != "125-------------_jpg.rf.9a0d958c405ffb596b4022e13f0686b7.jpg":
+        #     continue
+        for n in range(len(list_labels)):
+            label = list_labels[n]
+            label_name = label.split(".")[0]
+            if img_name != label_name:
+                continue
+            if img_name == label_name:
+                print(image)
+                print(label)
+                print("===========")
+                # 每张图都创建一个md文件
+                mdFile = MdUtils(file_name=md_path+'yq_0819_' + str(img_name), title='yq_0819_' + str(img_name) + '_iou>=0.5')
+                list_item = ["ori_cla", "predict_cla", "cut_ori_img", "cut_predict_img", "isTrue"]
+
+                result = send_requests(images_path + image)
+
+                img = cv2.imread(images_path + image)
+                img_h, img_w, _ = img.shape
+                # 获取原图上的切割结果
+                arr_label = []  # labels的边界框,存储四个顶点坐标
+                ori_clas = []  # label的真实类别
+                with open(labels_path + label, mode="r", encoding="utf-8") as f:
+                    lines = f.readlines()
+                for line in lines:
+                    line = line.split(" ")
+                    lrbt, c = xywh2lrbt(img_w, img_h, line)
+                    arr_label.append(lrbt)
+                    ori_clas.append(c)
+
+                all_label_len += len(lines)  # 所有标签的数量,为了后面计算所有图片的准确度
+
+                num = 0
+                for i in range(len(arr_label)):
+                    lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
+                    rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
+                    rect1 = [lt_x, lt_y, rb_x, rb_y]
+
+                    # 切割真实标签,并保存到本地
+                    cut_ori_img = img[lt_y:rb_y, lt_x:rb_x, :]
+                    save_cut_ori_img = cut_ori_img_path + str(img_name) + "_" + str(num) + ".jpg"
+                    cv2.imwrite(save_cut_ori_img, cut_ori_img)
+                    save_cut_ori_img = mdFile.new_inline_image(text='', path=md_cut_ori_img_path+ str(img_name) + "_" + str(num) + ".jpg")
+                    ori_cla = int(ori_clas[i])
+                    print("此时的ori_cla:", d_map[ori_cla])
+                    num += 1
+
+                    for j in range(len(result[0])):
+                        res = []
+                        predict_cla = int(result[0][j]['class'])
+                        print("此时的predict_cla:", d_map[predict_cla])
+                        if ori_cla == predict_cla or (ori_cla in [2, 3, 4] and predict_cla in [2, 3, 4]):
+                            rect2 = result[0][j]['bbox']
+                            iou = IOU(rect1, rect2)
+                            if iou >= 0.5:
+                                # 切割预测结果
+                                cut_predict_img = img[rect2[1]:rect2[3], rect2[0]:rect2[2], :]
+                                save_cut_predict_img = cut_predict_img_path + str(img_name) + "_" + str(
+                                    j) + ".jpg"
+                                cv2.imwrite(save_cut_predict_img, cut_predict_img)
+                                save_cut_predict_img = mdFile.new_inline_image(text='', path=md_cut_predict_img_path+ str(img_name) + "_" + str(j) + ".jpg")
+                                isTrue = True
+                                list_item.extend(
+                                    [d_map[ori_cla], d_map[predict_cla], save_cut_ori_img, save_cut_predict_img,
+                                     isTrue])
+                                each_acc_map[ori_cla] += 1
+                                all_acc += 1
+                                each_acc += 1
+                                break
+                        if j == len(result[0]) - 1:
+                            isTrue = False
+                            list_item.extend([d_map[ori_cla], " ", save_cut_ori_img, " ", isTrue])
+                            list_item.extend(res)
+
+                # 每一张图片的总准确度
+                each_acc = round(each_acc / len(ori_clas), 3)
+                # 每一类的准确度
+                code = round(each_acc_map[0] / ori_clas.count(0), 3) if ori_clas.count(0) else "这张图没有这个标签"
+                logo = round(each_acc_map[1] / ori_clas.count(1), 3) if ori_clas.count(1) else "这张图没有这个标签"
+                style = round(each_acc_map[2] / ori_clas.count(2), 3) if ori_clas.count(2) else "这张图没有这个标签"
+                table = round(each_acc_map[3] / ori_clas.count(3), 3) if ori_clas.count(3) else "这张图没有这个标签"
+                text = round(each_acc_map[4] / ori_clas.count(4), 3) if ori_clas.count(4) else "这张图没有这个标签"
+                title = round(each_acc_map[5] / ori_clas.count(5), 3) if ori_clas.count(5) else "这张图没有这个标签"
+
+                mdFile.new_line(str(image) + "—总准确率:" + str(each_acc))
+                mdFile.new_line(str(d_map[0]) + "—准确率:" + str(code))
+                mdFile.new_line(str(d_map[1]) + "—准确率:" + str(logo))
+                mdFile.new_line(str(d_map[2]) + "—准确率:" + str(style))
+                mdFile.new_line(str(d_map[3]) + "—准确率:" + str(table))
+                mdFile.new_line(str(d_map[4]) + "—准确率:" + str(text))
+                mdFile.new_line(str(d_map[5]) + "—准确率:" + str(title))
+
+                break
+
+        mdFile.new_line()
+        mdFile.new_table(columns=5, rows=len(list_item) // 5, text=list_item, text_align='center')
+        mdFile.create_md_file()
+
+    print(all_acc, all_label_len)
+    print("所有图片的预测准确度:", all_acc / all_label_len)

+ 110 - 0
cutimgs.py

@@ -0,0 +1,110 @@
+import cv2
+import os
+import matplotlib.pyplot as plt
+
+path = "./imgs/"
+cut_img_path = "./cut_img/"
+d_map = {0: 'code', 1: 'logo', 2: 'style', 3: 'table', 4: 'text', 5: 'title'}
+
+def makedir(dir):
+    if not os.path.exists(dir):
+        os.makedirs(dir)
+
+def xywh2lrbt(img_w, img_h, box):
+    c, x, y, w, h = int(box[0]), float(box[1]), float(box[2]), float(box[3]), float(box[4])
+    print(c, x, y, w, h)
+    print("+++++++++++++++++")
+    x = x * img_w  # 中心坐标x
+    w = w * img_w  # box的宽
+    y = y * img_h  # 中心坐标y
+    h = h * img_h  # box的高
+    print(c, x, y, w, h)
+
+    lt_x, lt_y = max(0, x - w / 2), max(0, y - h / 2)  # left_top_x, left_top_y
+    lb_x, lb_y = max(0, x - w / 2), min(img_h, y + h / 2)  # left_bottom_x, left_bottom_y
+    rb_x, rb_y = min(img_w, x + w / 2), min(img_h, y + h / 2)  # right_bottom_x, right_bottom_y
+    rt_x, rt_y = min(img_w, x + w / 2), max(0, y - h / 2)  # right_top_x, right_bottom_y
+
+    lrbt = [[lt_x, lt_y], [lb_x, lb_y], [rb_x, rb_y], [rt_x, rt_y]]
+    print(lrbt)
+
+    return lrbt, c
+
+
+img_cls = os.listdir(path)  # 获取train,valid,test目录名称
+print(img_cls)
+# 遍历每个目录,拿到图片和标签
+for img_cla in img_cls:
+    # print(img_cla)
+    images_path = path + img_cla + "/images/"
+    labels_path = path + img_cla + "/labels/"
+    list_images = os.listdir(images_path)
+    list_labels = os.listdir(labels_path)
+
+    for m in range(len(list_images)):
+        image = list_images[m]
+        img_name = os.path.splitext(image)[0]
+        if image != "784_jpg.rf.8a0d3a961866d9bda06a53d9bf2fbc40.jpg":
+            continue
+        for n in range(len(list_labels)):
+            label = list_labels[n]
+            label_name = os.path.splitext(label)[0]
+
+            if img_name != label_name:
+                continue
+            else:
+                # print(image)
+                # print(label)
+                # print(img_cla)
+                # print("==================")
+
+                img = cv2.imread(images_path + image)
+                img_h, img_w, _ = img.shape
+                arr_label = []  # labels的边界框,存储四个顶点坐标
+                ori_clas = []  # label的真实类别
+                with open(labels_path + label, mode="r", encoding="utf-8") as f:
+                    lines = f.readlines()
+
+                # some labels are [],-----> image not idea
+                if not lines:
+                    break
+
+                for line in lines:
+                    line = line.split(" ")
+                    lrbt, c = xywh2lrbt(img_w, img_h, line)
+                    arr_label.append(lrbt)
+                    ori_clas.append(c)
+
+                num = 0
+                for i in range(len(arr_label)):
+                    lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
+                    rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
+
+                    cls_num = ori_clas[i]
+                    cls = d_map[cls_num]
+                    print(cls)
+                    makedir(cut_img_path + cls)
+
+                    # 切割真实标签,并保存到本地
+                    # print(lt_y)
+                    # print(rb_y)
+                    # print(lt_x)
+                    # print(rb_x)
+                    if lt_x >= rb_x or lt_y >= rb_y:
+                        continue
+                    cut_ori_img = img[lt_y:rb_y, lt_x:rb_x, :]
+                    # plt.imshow(cut_ori_img[:,:,::-1])
+                    # plt.show()
+                    save_cut_ori_img = cut_img_path + cls + "/" + str(img_cla) + "_" + str(m) + "_" + str(cls) + "_" + str(num) + ".jpg"
+                    # cv2.imwrite(save_cut_ori_img, cut_ori_img)
+                    num += 1
+
+                break
+
+
+
+
+
+
+
+

+ 2 - 1
docker-compose.yml

@@ -10,7 +10,8 @@ services:
     tty: true
     tty: true
     working_dir: /workspace
     working_dir: /workspace
     ports:
     ports:
-      - '18089:8080'
+      - '4869:8080'
+      - '4868:22'
     # volumes:
     # volumes:
     #   - ./:/workspace
     #   - ./:/workspace
     # deploy:
     # deploy:

BIN
images/01.jpg


BIN
images/bus.jpg


BIN
images/city_street.jpg


BIN
images/zidane.jpg


+ 1 - 1
requirements.txt

@@ -36,4 +36,4 @@ pandas
 # extras --------------------------------------
 # extras --------------------------------------
 thop  # FLOPS computation
 thop  # FLOPS computation
 pycocotools>=2.0  # COCO mAP
 pycocotools>=2.0  # COCO mAP
-torchvision>=0.8.1
+torchvision>=0.8.1

+ 6 - 11
server.py

@@ -1,4 +1,3 @@
-# -*- coding:utf-8 -*-
 from fastapi import FastAPI, Request, Form, File, UploadFile
 from fastapi import FastAPI, Request, Form, File, UploadFile
 from fastapi.templating import Jinja2Templates
 from fastapi.templating import Jinja2Templates
 from pydantic import BaseModel
 from pydantic import BaseModel
@@ -18,6 +17,7 @@ YOLO_DIR = '/workspace/yolov5'
 # WEIGHTS = '/workspace/best.pt'
 # WEIGHTS = '/workspace/best.pt'
 # WEIGHTS = '/workspace/yili.pt'
 # WEIGHTS = '/workspace/yili.pt'
 # WEIGHTS = '/workspace/best2.pt'
 # WEIGHTS = '/workspace/best2.pt'
+
 WEIGHTS = '/workspace/0822yili.pt'
 WEIGHTS = '/workspace/0822yili.pt'
 
 
 logger = logging.getLogger('log')
 logger = logging.getLogger('log')
@@ -43,7 +43,7 @@ logger.addHandler(ch)
 
 
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 bl = torch.cuda.is_available()
 bl = torch.cuda.is_available()
-#logger.info(f'是否可使用GPU=======>{bl}')
+logger.info(f'是否可使用GPU=======>{bl}')
 
 
 app = FastAPI()
 app = FastAPI()
 templates = Jinja2Templates(directory = 'templates')
 templates = Jinja2Templates(directory = 'templates')
@@ -56,7 +56,6 @@ colors = [tuple([random.randint(0, 255) for _ in range(3)]) for _ in range(100)]
 
 
 if model_dict['ocr-layout'] is None:
 if model_dict['ocr-layout'] is None:
           model_dict['ocr-layout'] = model = torch.hub.load(YOLO_DIR, 'custom', path=WEIGHTS, source='local').to(device)
           model_dict['ocr-layout'] = model = torch.hub.load(YOLO_DIR, 'custom', path=WEIGHTS, source='local').to(device)
-#          print(model_dict['ocr-layout'])
           logger.info("========>模型加载成功")
           logger.info("========>模型加载成功")
 
 
 
 
@@ -93,7 +92,7 @@ def drag_and_drop_detect(request: Request):
 def detect_via_web_form(request: Request,
 def detect_via_web_form(request: Request,
                         file_list: List[UploadFile] = File(...),
                         file_list: List[UploadFile] = File(...),
                         model_name: str = Form(...),
                         model_name: str = Form(...),
-                        img_size: int = Form(800)):
+                        img_size: int = Form(1824)):
 
 
     '''
     '''
     Requires an image file upload, model name (ex. yolov5s). Optional image size parameter (Default 1824).
     Requires an image file upload, model name (ex. yolov5s). Optional image size parameter (Default 1824).
@@ -111,8 +110,7 @@ def detect_via_web_form(request: Request,
     #create a copy that corrects for cv2.imdecode generating BGR images instead of RGB
     #create a copy that corrects for cv2.imdecode generating BGR images instead of RGB
     #using cvtColor instead of [...,::-1] to keep array contiguous in RAM
     #using cvtColor instead of [...,::-1] to keep array contiguous in RAM
     img_batch_rgb = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in img_batch]
     img_batch_rgb = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in img_batch]
-    print(img_size)
-    print('111111')
+
     results = model_dict[model_name](img_batch_rgb, size = img_size)
     results = model_dict[model_name](img_batch_rgb, size = img_size)
 
 
     json_results = results_to_json(results,model_dict[model_name])
     json_results = results_to_json(results,model_dict[model_name])
@@ -142,7 +140,7 @@ def detect_via_web_form(request: Request,
 def detect_via_api(request: Request,
 def detect_via_api(request: Request,
                 file_list: List[UploadFile] = File(...),
                 file_list: List[UploadFile] = File(...),
                 model_name: str = Form(...),
                 model_name: str = Form(...),
-                img_size: Optional[int] = Form(800),
+                img_size: Optional[int] = Form(1920),
                 download_image: Optional[bool] = Form(False)):
                 download_image: Optional[bool] = Form(False)):
 
 
     '''
     '''
@@ -162,10 +160,7 @@ def detect_via_api(request: Request,
     #create a copy that corrects for cv2.imdecode generating BGR images instead of RGB,
     #create a copy that corrects for cv2.imdecode generating BGR images instead of RGB,
     #using cvtColor instead of [...,::-1] to keep array contiguous in RAM
     #using cvtColor instead of [...,::-1] to keep array contiguous in RAM
     img_batch_rgb = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in img_batch]
     img_batch_rgb = [cv2.cvtColor(img, cv2.COLOR_BGR2RGB) for img in img_batch]
-    print('111111')
-    print(img_size)
-    img_size= 800
-    print(img_size)
+
     results = model_dict[model_name](img_batch_rgb, size = img_size)
     results = model_dict[model_name](img_batch_rgb, size = img_size)
     json_results = results_to_json(results,model_dict[model_name])
     json_results = results_to_json(results,model_dict[model_name])
 
 

BIN
test_imgs/.DS_Store


BIN
test_imgs/images/01.jpg


BIN
test_imgs/images/02.jpg


BIN
test_imgs/images/03.jpg


BIN
test_imgs/images/04.jpg


BIN
test_imgs/images/05.jpg


+ 27 - 0
test_imgs/labels/01.txt

@@ -0,0 +1,27 @@
+2 0.9015390252839869 0.40270578968097676 0.15897764748992305 0.12198897203623474
+2 0.12520703554415535 0.4054430878298543 0.11080615610113594 0.11905474596297753
+3 0.2789153536093807 0.43694761717211505 0.15605716379626236 0.1442181961402127
+4 0.7265335287651155 0.4378810555336747 0.16112495419567607 0.24911776289877904
+5 0.4996775375595456 0.45831429696731 0.2141223891535361 0.14699094131547852
+2 0.9089739831440089 0.5284245766049626 0.13862953462806887 0.11057502953918866
+2 0.1255661414437523 0.5302796376526192 0.1115243679003298 0.11231193383221742
+1 0.4928032246244045 0.5507640803465932 0.03995236350311469 0.03959826703426546
+1 0.42574935873946496 0.5712721543914927 0.06039941370465372 0.06589602205592753
+4 0.2807621839501649 0.5893619535250099 0.15584096738732137 0.16515951161874753
+1 0.14955294979846098 0.6205947223316266 0.05776474899230487 0.06033871602993304
+1 0.5632979113228289 0.6240055139818826 0.09681201905459876 0.11879086254430879
+4 0.9551264199340418 0.624671130366286 0.03138145840967387 0.03600630169358015
+4 0.09986075485525833 0.6260259944860181 0.0366397947966288 0.03662071681764474
+0 0.7260754855258337 0.6266482867270579 0.1582521069989007 0.12618353682552186
+4 0.9577720776841334 0.6712997243009059 0.02884939538292415 0.04095313115399764
+4 0.12024184683034078 0.6721425758172509 0.07935507511909125 0.04052776683733754
+4 0.42443752290216197 0.6773060259944861 0.09104067423964822 0.03614415124064592
+4 0.8655734701355808 0.6776486805829066 0.08313301575668744 0.024363922804253643
+1 0.21567973616709418 0.1499369830641985 0.05060828142176622 0.06125640015754234
+4 0.782103334554782 0.154883812524616 0.060971051667277384 0.06163450177235132
+4 0.5012165628435324 0.1885191020086648 0.14333455478197143 0.03263489562820008
+4 0.9373506779039942 0.19026782197715636 0.05257237083180652 0.020949192595510042
+1 0.5020520337119824 0.2776092949980307 0.19535360938072555 0.11387554155179204
+1 0.1301209234151704 0.27997243009058687 0.10836203737632834 0.10729814887751084
+2 0.903081714913888 0.28357227254824735 0.15814950531330157 0.10031902323749509
+4 0.25807255404910223 0.3398227648680583 0.11633198973983146 0.03618353682552186

+ 27 - 0
test_imgs/labels/02.txt

@@ -0,0 +1,27 @@
+2 0.9015390252839869 0.40270578968097676 0.15897764748992305 0.12198897203623474
+2 0.1271528032246244 0.40813706183536824 0.11469769146207402 0.12444269397400551
+4 0.7261744228655185 0.44918077983458055 0.16040674239648223 0.2717172115005908
+5 0.4996775375595456 0.45831429696731 0.2141223891535361 0.14699094131547852
+3 0.2785489190179553 0.4670460811343048 0.15605716379626236 0.1442181961402127
+2 0.9004946866984243 0.5296691610870421 0.15558446317332356 0.10808979913351713
+2 0.1255661414437523 0.5302796376526192 0.1115243679003298 0.11231193383221742
+1 0.4928032246244045 0.5507640803465932 0.03995236350311469 0.03959826703426546
+1 0.42574935873946496 0.5712721543914927 0.06039941370465372 0.06589602205592753
+4 0.2794210333455478 0.6125797558093737 0.1554598754122389 0.1468964159117763
+1 0.14955294979846098 0.6205947223316266 0.05776474899230487 0.06033871602993304
+1 0.5632979113228289 0.6240055139818826 0.09681201905459876 0.11879086254430879
+4 0.9551264199340418 0.624671130366286 0.03138145840967387 0.03600630169358015
+4 0.09986075485525833 0.6260259944860181 0.0366397947966288 0.03662071681764474
+0 0.7255734701355808 0.6376841276092949 0.157248076218395 0.10410791650256006
+4 0.9577720776841334 0.6712997243009059 0.02884939538292415 0.04095313115399764
+4 0.12024184683034078 0.6721425758172509 0.07935507511909125 0.04052776683733754
+4 0.42443752290216197 0.6773060259944861 0.09104067423964822 0.03614415124064592
+4 0.8655734701355808 0.6776486805829066 0.08313301575668744 0.024363922804253643
+1 0.21567973616709418 0.1499369830641985 0.05060828142176622 0.06125640015754234
+4 0.782103334554782 0.154883812524616 0.060971051667277384 0.06163450177235132
+4 0.5012165628435324 0.1885191020086648 0.14333455478197143 0.03263489562820008
+4 0.9373506779039942 0.19026782197715636 0.05257237083180652 0.020949192595510042
+1 0.5020520337119824 0.2776092949980307 0.19535360938072555 0.11387554155179204
+1 0.12585928911689265 0.28114612051988974 0.10836203737632834 0.10729814887751084
+2 0.903081714913888 0.28357227254824735 0.15814950531330157 0.10031902323749509
+4 0.2814730670575302 0.3555612445844821 0.15768046903627703 0.07435604568727845

+ 19 - 0
test_imgs/labels/03.txt

@@ -0,0 +1,19 @@
+4 0.21092948717948717 0.374340995053826 0.039067599067599074 0.014262438172825138
+3 0.21183275058275058 0.4295082921152168 0.0810576923076923 0.09298225196392201
+0 0.3031410256410257 0.47855106197265057 0.06031468531468531 0.1229735234215886
+4 0.2119871794871795 0.512737852778586 0.0850174825174825 0.07218795461157987
+4 0.4220775058275058 0.5193860925225487 0.013243006993006994 0.05630200756473669
+4 0.8641695804195804 0.534887983706721 0.0559965034965035 0.027390165842304335
+2 0.5031177156177157 0.5355600814663951 0.09374125874125874 0.03150130928135001
+4 0.5966083916083916 0.5386645330229852 0.08832459207459208 0.021905731742798955
+1 0.20175116550116548 0.18753564154786148 0.07063228438228439 0.05163514693046261
+2 0.8318851981351982 0.2160837940064009 0.04932983682983683 0.10572010474250801
+2 0.7959994172494173 0.21695955775385511 0.016526806526806527 0.06963922025021821
+2 0.7587004662004663 0.21790514983997675 0.019874708624708623 0.06936863543788187
+2 0.7202068764568764 0.23552807681117252 0.01865967365967366 0.10137038114634857
+4 0.7793181818181818 0.245010183299389 0.012392191142191143 0.10520221123072447
+4 0.7021124708624709 0.25278149549025314 0.01386946386946387 0.12506546406750074
+4 0.7421182983682983 0.2661798079720687 0.011608391608391609 0.14861798079720687
+4 0.5724475524475525 0.2722897876054699 0.014434731934731935 0.12386965376782078
+5 0.5188111888111888 0.28004364271166715 0.1105128205128205 0.26218795461157984
+4 0.25699009324009325 0.28994471923188825 0.1774562937062937 0.1543148094268257

+ 23 - 0
test_imgs/labels/04.txt

@@ -0,0 +1,23 @@
+2 0.9087275449101796 0.40016541945647893 0.11264221556886228 0.1231744781410004
+3 0.2857784431137725 0.4036274123670736 0.1459693113772455 0.12429696730996455
+2 0.10357035928143712 0.4045844820795589 0.1486002994011976 0.1253564395431272
+5 0.5141504491017964 0.46024812918471836 0.21870134730538923 0.15662465537613235
+4 0.7409730538922156 0.5048011027963766 0.17294535928143714 0.38798345805435214
+4 0.27876497005988027 0.517093343836156 0.14128742514970058 0.10336746750689248
+2 0.9115980538922156 0.5259472233162663 0.10823727544910179 0.10866089011421819
+2 0.10225299401197606 0.5291492713666798 0.1424176646706587 0.11150059078377315
+1 0.5764408682634731 0.616423788893265 0.09887724550898203 0.11879086254430879
+1 0.1527432634730539 0.6205947223316266 0.058997005988023946 0.06033871602993304
+4 0.09265718562874252 0.6296455297361166 0.06111152694610778 0.03662071681764474
+0 0.2827919161676647 0.6440173296573455 0.15682634730538922 0.10556912170145727
+4 0.12400449101796407 0.6706183536825522 0.12190119760479043 0.03979125640015754
+4 0.43349176646706583 0.6773060259944861 0.09298278443113772 0.03614415124064592
+4 0.8840381736526947 0.6776486805829066 0.08490643712574851 0.024363922804253643
+1 0.23206586826347306 0.12258369436786136 0.07332335329341316 0.07564789287120914
+4 0.7973353293413173 0.12858605750295393 0.06583083832335329 0.021638440330838914
+4 0.9175935628742514 0.18396218983851909 0.0536938622754491 0.020949192595510042
+4 0.5119086826347305 0.1885191020086648 0.14639221556886228 0.03263489562820008
+1 0.5127619760479042 0.2776092949980307 0.19952095808383233 0.11387554155179204
+1 0.10677769461077845 0.27968491532099254 0.1556062874251497 0.10754233950374163
+2 0.9093937125748504 0.28222922410397794 0.11602919161676646 0.1006695549428909
+4 0.2884842814371258 0.3261835368255218 0.1565306886227545 0.027924379677038206

+ 27 - 0
test_imgs/labels/05.txt

@@ -0,0 +1,27 @@
+2 0.9015390252839869 0.40270578968097676 0.15897764748992305 0.12198897203623474
+2 0.12520703554415535 0.4054430878298543 0.11080615610113594 0.11905474596297753
+4 0.7280505679736167 0.450106341079165 0.1641626969585929 0.27356833398975977
+5 0.4996775375595456 0.45831429696731 0.2141223891535361 0.14699094131547852
+3 0.27911322828875046 0.465959038991729 0.15640161231220226 0.1442181961402127
+2 0.9089739831440089 0.5284245766049626 0.13862953462806887 0.11057502953918866
+2 0.1255661414437523 0.5302796376526192 0.1115243679003298 0.11231193383221742
+1 0.4928032246244045 0.5507640803465932 0.03995236350311469 0.03959826703426546
+1 0.42574935873946496 0.5712721543914927 0.06039941370465372 0.06589602205592753
+4 0.27994869915720044 0.6077471445450965 0.15517039208501282 0.13965340685309177
+1 0.14955294979846098 0.6205947223316266 0.05776474899230487 0.06033871602993304
+1 0.5632979113228289 0.6240055139818826 0.09681201905459876 0.11879086254430879
+4 0.9551264199340418 0.624671130366286 0.03138145840967387 0.03600630169358015
+4 0.09986075485525833 0.6260259944860181 0.0366397947966288 0.03662071681764474
+0 0.7241993404177354 0.6386293816463174 0.15809820447050202 0.10563607719574634
+4 0.9577720776841334 0.6712997243009059 0.02884939538292415 0.04095313115399764
+4 0.12024184683034078 0.6721425758172509 0.07935507511909125 0.04052776683733754
+4 0.42443752290216197 0.6773060259944861 0.09104067423964822 0.03614415124064592
+4 0.8655734701355808 0.6776486805829066 0.08313301575668744 0.024363922804253643
+1 0.21567973616709418 0.1499369830641985 0.05060828142176622 0.06125640015754234
+4 0.782103334554782 0.154883812524616 0.060971051667277384 0.06163450177235132
+4 0.5012165628435324 0.1885191020086648 0.14333455478197143 0.03263489562820008
+4 0.9373506779039942 0.19026782197715636 0.05257237083180652 0.020949192595510042
+1 0.5020520337119824 0.2776092949980307 0.19535360938072555 0.11387554155179204
+1 0.1301209234151704 0.27997243009058687 0.10836203737632834 0.10729814887751084
+2 0.903081714913888 0.28357227254824735 0.15814950531330157 0.10031902323749509
+4 0.27973250274825945 0.35714060653800705 0.15549651887138147 0.07602599448601811

+ 180 - 0
writeMD.py

@@ -0,0 +1,180 @@
+from mdutils.mdutils import MdUtils
+import os
+import cv2
+import numpy as np
+import requests
+
+
+# url = "http://192.168.199.249:4869"
+
+# 标签是经过归一化的,需要变回来
+# xywh格式 ---> box四个顶点的坐标
+def xywh2lrbt(img_w, img_h, box):
+    c, x, y, w, h = float(box[0]), float(box[1]), float(box[2]), float(box[3]), float(box[4])
+    x = x * img_w  # 中心坐标x
+    w = w * img_w  # box的宽
+    y = y * img_h  # 中心坐标y
+    h = h * img_h  # box的高
+
+    lt_x, lt_y = x - w / 2, y - h / 2  # left_top_x, left_top_y
+    lb_x, lb_y = x - w / 2, y + h / 2  # left_bottom_x, left_bottom_y
+    rb_x, rb_y = x + w / 2, y + h / 2  # right_bottom_x, right_bottom_y
+    rt_x, rt_y = x + w / 2, y - h / 2  # right_top_x, right_bottom_y
+
+    lrbt = [[lt_x, lt_y], [lb_x, lb_y], [rb_x, rb_y], [rt_x, rt_y]]
+
+    return lrbt, c
+
+
+def IOU(rect1, rect2):
+    xmin1, ymin1, xmax1, ymax1 = rect1
+    xmin2, ymin2, xmax2, ymax2 = rect2
+    s1 = (xmax1 - xmin1) * (ymax1 - ymin1)
+    s2 = (xmax2 - xmin2) * (ymax2 - ymin2)
+
+    sum_area = s1 + s2
+
+    left = max(xmin2, xmin1)
+    right = min(xmax2, xmax1)
+    top = max(ymin2, ymin1)
+    bottom = min(ymax2, ymax1)
+
+    if left >= right or top >= bottom:
+        return 0
+
+    intersection = (right - left) * (bottom - top)
+    return intersection / (sum_area - intersection) * 1.0
+
+
+def send_requests():
+    return result
+
+img_path = "./images/test1.jpg"
+b_img_list = []
+b_img_list.append(('file_list', ('image.jpeg', open(img_path, 'rb'), 'image/jpeg')))
+payload = {'model_name': 'ocr-layout', 'img_size': 800, 'download_image': False}
+response = requests.request("POST", "http://192.168.199.107:18089/detect", headers={}, data=payload, files=b_img_list)
+result = eval(str(response.text))
+result = eval(str(result['result']))
+# print(len(result))
+
+mdFile = MdUtils(file_name='yolov5_yili_0818_1', title='Markdown File Example')
+mdFile.new_header(level=1, title="yolov5预测结果")
+list_item = ["ori_cla", "predict_cla", "cut_ori_img", "cut_predict_img", "isTrue"]
+# list_item.extend(["ori_cla", "predict_cla", "cut_ori_img", "cut_predict_img", "isTrue"])
+# for i in range(len(result[0])):
+#     # print(stencil_list[0][i])
+#
+#     predict_cla = result[0][i]["class"]
+
+# 获取真实的标签
+label = "./images/test1.txt"
+with open(label, mode="r", encoding="utf-8") as f:
+    lines = f.readlines()
+
+img = cv2.imread(img_path)
+img_h, img_w, _ = img.shape
+
+arr_label = []  # labels的边界框,存储四个顶点坐标
+ori_clas = []  # label的真实类别
+for line in lines:
+    line = line.split(" ")
+    lrbt, c = xywh2lrbt(img_w, img_h, line)
+    arr_label.append(lrbt)
+    ori_clas.append(c)
+
+# cut_imgs = []  # 被剪切的图像
+num = 0
+for i in range(len(arr_label)):
+    lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
+    rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
+    rect1 = [lt_x, lt_y, rb_x, rb_y]
+
+    cut_ori_img = img[lt_y:rb_y, lt_x:rb_x, :]
+    save_cut_ori_img = "./images/cut_imgs/" + str(i) + "_" + str(num) + ".jpg"
+    cv2.imwrite(save_cut_ori_img, cut_ori_img)
+    save_cut_ori_img = mdFile.new_inline_image(text='', path=save_cut_ori_img)
+    num += 1
+    ori_cla = ori_clas[i]
+    for j in range(len(result[0])):
+        res = []
+        predict_cla = result[0][j]['class']
+        if ori_cla == predict_cla:
+            rect2 = result[0][j]['bbox']
+            iou = IOU(rect1, rect2)
+            if iou >= 0.5:
+                cut_predict_img = img[rect2[1]:rect2[3], rect2[0]:rect2[2], :]
+                save_cut_predict_img = "./images/cut_imgs/" + str(j) + ".jpg"
+                cv2.imwrite(save_cut_predict_img, cut_predict_img)
+                save_cut_predict_img = mdFile.new_inline_image(text='', path=save_cut_predict_img)
+                isTrue = True
+                list_item.extend([ori_cla, predict_cla, save_cut_ori_img, save_cut_predict_img, isTrue])
+                # list_item.extend(res)
+                break
+        elif j == len(result[0]) - 1:
+            isTrue = False
+            list_item.extend([ori_cla, predict_cla, save_cut_ori_img, "", isTrue])
+            # res.append([ori_cla, predict_cla, "", "", isTrue])
+            list_item.extend(res)
+#
+# print(list_item)
+# print(len(arr_label))
+print(len(list_item))
+# print(len(result[0]))
+mdFile.new_line()
+mdFile.new_table(columns=5, rows=len(list_item)//5, text=list_item, text_align='center')
+mdFile.create_md_file()
+
+if __name__ == "__main__":
+    images_path = "./test_imgs/images/"
+    labels_path = "./test_imgs/labels/"
+    list_images = os.listdir(images_path)
+    list_labels = os.listdir(labels_path)
+    for image in list_images:
+        img_name = image.split(".")[0]
+        mdFile = MdUtils(file_name='yq_0819_' + str(img_name), title='yq_0819_' + str(img_name) + '_iou>=0.5')
+
+
+
+
+#
+# images = os.listdir(images_path)
+# labels = os.listdir(labels_path)
+#
+# assert len(images) == len(labels)
+#
+# for i in range(len(images)):
+#     image = images[i]
+#     label = labels[i]
+#     with open(labels_path + label, mode="r", encoding="utf-8") as f:
+#         lines = f.readlines()
+#
+#     img = images_path + images[i]
+#     img = cv2.imread(img)
+#     img_h, img_w, _ = img.shape
+#
+#     arr_label = []  # labels的边界框,存储四个顶点坐标
+#     ori_cla = []  # label的真实类别
+#     for line in lines:
+#         line = line.split(" ")
+#         lrbt, c = xywh2lrbt(img_w, img_h, line)
+#         arr_label.append(lrbt)
+#         ori_cla.append(c)
+#
+#     cut_path = "./valid_img/cut/"
+#
+#     for i in range(len(arr_label)):
+#         lt_x, lt_y = int(arr_label[i][0][0]), int(arr_label[i][0][1])
+#         rb_x, rb_y = int(arr_label[i][2][0]), int(arr_label[i][2][1])
+#         cut_img = img[lt_y:rb_y, lt_x:rb_x, :]
+#         cv2.imwrite(cut_path + str(i) + ".jpg", cut_img)
+#
+#         box = np.reshape(np.array(arr_label[i]), [-1, 1, 2]).astype(np.int32)
+#         img = cv2.polylines(np.array(img), [box], True, (255, 0, 0), 5)
+#
+#
+#
+#
+# mdFile.create_md_file()
+#
+#