Raychar 2 years ago
parent
commit
f9e641d1b0

BIN
.DS_Store


+ 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>

+ 2 - 1
Dockerfile

@@ -102,7 +102,8 @@ RUN git clone https://gitee.com/monkeycc/yolov5.git
 # RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/yili.pt
 # RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/best.pt
 # RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/best2.pt
-RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/yili2.pt
+# RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/yili2.pt
+RUN wget --user=sxkj --password='sx' ftp://192.168.199.208/xxc_yolo_8.25.pt
 
 ADD . /workspace
 EXPOSE 8080

+ 2 - 1
docker-compose.yml

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

+ 2 - 2
server.py

@@ -17,7 +17,7 @@ YOLO_DIR = '/workspace/yolov5'
 # WEIGHTS = '/workspace/best.pt'
 # WEIGHTS = '/workspace/yili.pt'
 # WEIGHTS = '/workspace/best2.pt'
-WEIGHTS = '/workspace/yili2.pt'
+WEIGHTS = '/workspace/xxc_yolo_8.25.pt'
 
 logger = logging.getLogger('log')
 logger.setLevel(logging.DEBUG)
@@ -139,7 +139,7 @@ def detect_via_web_form(request: Request,
 def detect_via_api(request: Request,
                 file_list: List[UploadFile] = File(...),
                 model_name: str = Form(...),
-                img_size: Optional[int] = Form(1824),
+                img_size: Optional[int] = Form(1920),
                 download_image: Optional[bool] = Form(False)):
 
     '''

+ 122 - 0
test_addr.py

@@ -0,0 +1,122 @@
+# 重构代码
+import json
+import cpca
+
+addr = "江苏省京市永扬大道"
+
+f = open('areas.json', 'r')
+content = f.read()
+areas = json.loads(content)
+
+
+def get_cpca(addr):
+    df = cpca.transform([addr])
+    print(df)
+    province = df.iloc[0][0]
+    city = df.iloc[0][1] or ""
+    region = df.iloc[0][2] or ""
+    street = df.iloc[0][3]
+    print(province, city, region, street)
+    return province, city, region, street
+
+
+province, city, region, street = get_cpca(addr)
+
+
+def get_region(city_or_province, temp_addr):
+    """
+    flag=1:city_or_province is city
+    flag=0: city_or_province is province
+    """
+    maxnum = 0
+    for _, json_region in enumerate(city_or_province['children']):
+        num = 0
+        if len(temp_addr) == len(json_region['name']):
+            for i in range(len(temp_addr)):
+                if temp_addr[i] == json_region['name'][i]:
+                    num += 1
+        elif len(temp_addr) < len(json_region['name']):
+            for i in range(len(temp_addr)):
+                for j in range(len(json_region['name'])):
+                    if temp_addr[i] == json_region['name'][j]:
+                        num += 1
+                        break
+        if maxnum <= num:
+            maxnum = num
+            region = json_region['name']
+
+    return region
+
+
+def cal_regions(json_province, city, temp_addr, flag):
+    """
+    flag=0:cpca没有找到"区",也没有找到"市",只需要找到市即可
+    flag=1:cpca没有找到"区",需要找到"区"
+    """
+    if flag == 0:
+        return get_region(json_province, temp_addr)
+    elif flag == 1:
+        for _, json_city in enumerate(json_province['children']):
+            if (city and city in json_city['name']) or (not city):
+                return get_region(json_city, temp_addr)
+
+
+def cal_addrs(province, city, region, temp_addr, areas, flag):
+    for _, json_province in enumerate(areas):
+        if province in json_province['name']:
+            region = cal_regions(json_province, city, temp_addr, flag)
+            return region
+
+
+# 第一步,判断 df中的省市区是否存在,存在说明后面的street只要把原来的省市区丢掉即可
+if province and city and region:
+    print("省市区都存在,只需要切割street中‘区’后面的内容")
+    if "区" in street:
+        street = street.split("市")[-1].split("区")[-1]
+    if "县" in street:
+        street = street.split("市")[-1].split("县")[-1]
+
+# 第二步,判断 df中“区”是否存在,如果存在,那么 “省市区” 肯定都存在
+elif not region:
+    # 判断 df中“市”是否存在,如果存在,那么 ”省市“ 肯定都存在
+    if city:
+        if '区' in street or '县' in street:
+            print("cpca没有检测到‘区’,但是检测到省,市,并且street中含有‘区’,需要校准区")
+            flag = 1
+            if "区" in street:
+                temp_region = street.split("区")[0] + "区"
+                street = street.split("区")[-1]
+            if "县" in street:
+                temp_region = street.split("县")[0] + "县"
+                street = street.split("县")[-1]
+            region = cal_addrs(province, city, region, temp_region, areas, flag)
+        else:
+            # (暂时)说明street没有必要切割了
+            print("cpca没有检测到‘区’,但是检测到省,市,并且street中没有有‘区’,就不需要校准区")
+
+    # 这里比较复杂
+    elif not city:
+        # 还是先校准“区”,如果区存在,就直接把区拿到,然后再进行一次cpca,
+        # 如果“区”不存在,就直接校准“市”,street直接切割
+        if '区' in street or '县' in street:
+            print("cpca只检测到了省,但是street中有区,直接分割出区,再做cpca即可")
+            flag = 1
+            if "区" in street:
+                temp_region = street.split('市')[-1].split('区')[0] + "区"
+                street = street.split('区')[-1]
+            if "县" in street:
+                temp_region = street.split('市')[-1].split('县')[0] + "县"
+                street = street.split('县')[-1]
+            region = cal_addrs(province, city, region, temp_region, areas, flag)
+        elif '市' not in street:
+            # 市,区都没有,那就直接返回
+            print("cpca只检测到了省,并且street中没有市,也没有区,直接跳过")
+        elif '市' in street:
+            print("cpca只检测到了省,并且street中有市,没有区,就只要校准city")
+            flag = 0
+            temp_city = street.split('市')[0] + '市'
+            street = street.split('市')[-1]
+            city = cal_addrs(province, city, region, temp_city, areas, flag)
+
+addr = province + city + region + street
+province, city, region, street = get_cpca(addr)

BIN
test_imgs/.DS_Store


+ 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()
+#
+#