Browse Source

-update: version beta1.0

individual detection strategy code work
not integrated yet
zbc 4 months ago
parent
commit
0be4b5a819
100 changed files with 1841 additions and 0 deletions
  1. 135 0
      count_env.py
  2. 22 0
      data/JSON/DSC_4604.json
  3. 30 0
      data/JSON_MASK/DSC_4604_mask.json
  4. BIN
      data/QUERY/DSC_4575.JPG
  5. BIN
      data/QUERY/DSC_4576.JPG
  6. BIN
      data/QUERY/DSC_4577.JPG
  7. BIN
      data/QUERY/DSC_4579.JPG
  8. BIN
      data/QUERY/DSC_4581.JPG
  9. BIN
      data/QUERY/DSC_4582.JPG
  10. BIN
      data/QUERY/DSC_4585.JPG
  11. BIN
      data/QUERY/DSC_4586.JPG
  12. BIN
      data/QUERY/DSC_4587.JPG
  13. BIN
      data/QUERY/DSC_4588.JPG
  14. BIN
      data/QUERY/DSC_4590.JPG
  15. BIN
      data/QUERY/DSC_4591.JPG
  16. BIN
      data/QUERY/DSC_4594.JPG
  17. BIN
      data/QUERY/DSC_4596.JPG
  18. BIN
      data/QUERY/DSC_4597.JPG
  19. BIN
      data/QUERY/DSC_4599.JPG
  20. BIN
      data/QUERY/DSC_4600.JPG
  21. BIN
      data/QUERY/DSC_4602.JPG
  22. BIN
      data/QUERY/DSC_4603.JPG
  23. BIN
      data/REF/DSC_4604.JPG
  24. BIN
      data/demo_613/1_image_query/DSC_0474.JPG
  25. BIN
      data/demo_613/2_image_query/ADSC_0485.JPG
  26. BIN
      data/demo_613/2_image_query/DSC_0476.JPG
  27. BIN
      data/demo_613/2_image_query/DSC_0479.JPG
  28. BIN
      data/demo_613/2_image_query/DSC_0482.JPG
  29. BIN
      data/demo_613/2_image_query/DSC_0488.JPG
  30. BIN
      data/demo_613/2_image_query/DSC_0492.JPG
  31. BIN
      data/demo_613/3_image_query/ADSC_0480.JPG
  32. BIN
      data/demo_613/3_image_query/DSC_0477.JPG
  33. BIN
      data/demo_613/3_image_query/DSC_0483.JPG
  34. BIN
      data/demo_613/3_image_query/DSC_0486.JPG
  35. BIN
      data/demo_613/3_image_query/DSC_0489.JPG
  36. BIN
      data/demo_613/3_image_query/DSC_0493.JPG
  37. 262 0
      data/demo_613/json/ADSC_0478.json
  38. 30 0
      data/demo_613/json/ADSC_0478_mask.json
  39. 118 0
      data/demo_613/json/ADSC_0480.json
  40. 30 0
      data/demo_613/json/ADSC_0480_mask.json
  41. 182 0
      data/demo_613/json/ADSC_0485.json
  42. 30 0
      data/demo_613/json/ADSC_0485_mask.json
  43. BIN
      data/demo_613/ref_image/ADSC_0478.JPG
  44. BIN
      data/demo_613/ref_image/ADSC_0480.JPG
  45. BIN
      data/demo_613/ref_image/ADSC_0485.JPG
  46. BIN
      data/images/one_pillar_pagoda_1.jpg
  47. BIN
      data/images/one_pillar_pagoda_2.jpg
  48. 262 0
      data/json/image165214-001.json
  49. 214 0
      data/json/image165214-019.json
  50. 166 0
      data/json/image165214-037.json
  51. 70 0
      data/json1/DSC_0449.json
  52. 30 0
      data/json1/DSC_0449_mask.json
  53. 70 0
      data/json1/DSC_0453.json
  54. 30 0
      data/json1/DSC_0453_mask.json
  55. 30 0
      data/mask_json/image165214-001_mask.json
  56. 30 0
      data/mask_json/image165214-019_mask.json
  57. 30 0
      data/mask_json/image165214-037_mask.json
  58. BIN
      data/new/DSC_4531.JPG
  59. BIN
      data/new/DSC_4532.JPG
  60. BIN
      data/new/DSC_4533.JPG
  61. BIN
      data/new/DSC_4534.JPG
  62. BIN
      data/new/DSC_4535.JPG
  63. BIN
      data/new/DSC_4536.JPG
  64. BIN
      data/new/DSC_4537.JPG
  65. BIN
      data/new/DSC_4538.JPG
  66. BIN
      data/new/DSC_4539.JPG
  67. BIN
      data/new/DSC_4540.JPG
  68. BIN
      data/new/DSC_4541.JPG
  69. BIN
      data/new/DSC_4542.JPG
  70. BIN
      data/new/DSC_4543.JPG
  71. BIN
      data/new/DSC_4544.JPG
  72. BIN
      data/new/DSC_4545.JPG
  73. BIN
      data/new/DSC_4546.JPG
  74. BIN
      data/new/DSC_4547.JPG
  75. BIN
      data/new/DSC_4548.JPG
  76. BIN
      data/new/DSC_4549.JPG
  77. BIN
      data/new/DSC_4550.JPG
  78. BIN
      data/new/DSC_4551.JPG
  79. BIN
      data/new/DSC_4552.JPG
  80. BIN
      data/new_images/DSC_4527.JPG
  81. BIN
      data/new_images/DSC_4528.JPG
  82. BIN
      data/new_images/DSC_4529.JPG
  83. BIN
      data/new_images/DSC_4530.JPG
  84. BIN
      data/new_images/DSC_4531.JPG
  85. BIN
      data/new_images/DSC_4532.JPG
  86. BIN
      data/new_images/DSC_4533.JPG
  87. BIN
      data/new_images/DSC_4534.JPG
  88. BIN
      data/new_images/DSC_4535.JPG
  89. BIN
      data/new_images/DSC_4536.JPG
  90. BIN
      data/new_images/DSC_4537.JPG
  91. BIN
      data/new_images/DSC_4538.JPG
  92. BIN
      data/new_images/DSC_4539.JPG
  93. BIN
      data/new_images/DSC_4540.JPG
  94. BIN
      data/new_images/DSC_4541.JPG
  95. BIN
      data/new_images/DSC_4542.JPG
  96. BIN
      data/new_images/DSC_4543.JPG
  97. BIN
      data/new_images/DSC_4544.JPG
  98. BIN
      data/new_images/DSC_4545.JPG
  99. BIN
      data/new_images/DSC_4546.JPG
  100. 70 0
      data/similarity_image/JSON_path/frame_000000.json

+ 135 - 0
count_env.py

@@ -0,0 +1,135 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+# @Time    : 2024/6/13 0013 下午 12:03
+# @Author  : liudan
+# @File    : count_env.py
+# @Software: pycharm
+import cv2
+import os
+from PIL import Image, ImageDraw
+import numpy as np
+
+from skimage.metrics import structural_similarity as compare_ssim
+import json
+import random
+import yaml
+import demo_env
+from demo_env import registration_demo
+
+
+def compare_boxes_similarity(image1_path, image2_path, json_file_path, similarity_threshold=0.4):
+    try:
+        if not os.path.exists(image1_path):
+            raise FileNotFoundError(f"Image file {image1_path} not found")
+
+
+        image1 = Image.open(image1_path)# 原图尺寸,未resize
+        image2 = wrap_image
+
+        draw1 = ImageDraw.Draw(image1)
+
+        # 存储相似度结果和是否相同的判断
+        similarity_results = []
+        same_content_boxes = []
+
+
+        with open(json_file_path, 'r') as f:
+            data = json.load(f)
+
+
+        for shape in data['shapes']:
+            if 'points' in shape:
+                shape['points'] = [[int(round(x)), int(round(y))] for x, y in shape['points']]
+                x1, y1 = shape['points'][0]
+                x2, y2 = shape['points'][1]
+
+            # 从两幅图像中截取对应区域
+            region1 = image1.crop((x1, y1, x2, y2))
+            # region1 = image1.crop((x1, y1, x2, y2)).convert('L')
+            draw1.rectangle([x1, y1, x2, y2], outline='red', width=2)
+            image1.save(os.path.join(params['save_dir'], f'save_annotated1_{i}.jpg'))
+            region1.save(os.path.join(params['save_dir'], f'111111{i}.jpg'))
+
+
+            # region2 = image2.crop((left-80, top, right-80, bottom))
+            region2 = image2[y1:y2, x1:x2]
+            # region2 = cv2.cvtColor(region2, cv2.COLOR_BGR2GRAY)
+            # region2= region2.transpose(Image.FLIP_TOP_BOTTOM) #旋转180°针对pillowImage对象
+            # region2 = cv2.rotate(region2, cv2.ROTATE_180)
+
+            filename = f'json_image_{shape["label"]}_{i}.jpg'
+            cv2.imwrite(os.path.join(params['save_dir'], filename), region2)
+            cv2.rectangle(image2, (x1, y1), (x2, y2),(0,255,0), 2)
+            cv2.imwrite(os.path.join(params['save_dir'], f'save_annotated2_{i}.jpg'), image2)
+
+            # 将PIL图像转换为numpy数组,以便进行计算
+            arr1 = np.array(region1)
+            arr2 = region2 # region2一直是numpy数组,所以上述image1和image2处理方式不同
+
+            # 确保两个数组的形状是相同的
+            assert arr1.shape == arr2.shape, "Images do not have the same size for the given box"
+
+            # 使用SSIM计算相似度(范围在-1到1之间,1表示完全相似)
+            # ssim = compare_ssim(arr1, arr2, multichannel=False) # 这是旧版,可以计算灰度图相似度,对于计算彩色图像即使设置multichannel=True也错
+            ssim = compare_ssim(arr1, arr2, channel_axis=2)
+
+
+            similarity_results.append(ssim)
+
+            if ssim > similarity_threshold:
+                same_content_boxes.append(shape)
+                cv2.rectangle(image2, (x1, y1), (x2, y2),(0,255,0), 2)
+
+                text = "Similarity: " + str(round(ssim, 3))
+                text_pos = (x1, y1 - 5)
+                # 参数:图像, 文本, 文本位置, 字体类型, 字体大小, 字体颜色, 字体粗细
+                cv2.putText(image2, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 2)
+                cv2.imwrite(os.path.join(params['visualization_dir'],f'{wrap_images_name[:-8]}_{i}.jpg'), image2)
+
+            else:
+                cv2.rectangle(image2, (x1, y1), (x2, y2), (0, 0, 255), 2)
+                text = "score: " + str(round(ssim, 3))
+                text_pos = (x1, y1 - 5)
+                # 参数:图像, 文本, 文本位置, 字体类型, 字体大小, 字体颜色, 字体粗细
+                cv2.putText(image2, text, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 0, 255), 2)
+                cv2.imwrite(os.path.join(params['visualization_dir'], f'{wrap_images_name[:-8]}_{i}.jpg'), image2)
+
+        return similarity_results, same_content_boxes
+
+    except FileNotFoundError as e:
+        print(f"An error occurred: {e}")
+    except Exception as e:
+        print(f"An unexpected error occurred: {e}")
+    return None, None
+
+
+def read_params_from_yml(yml_file_path):
+    with open(yml_file_path, 'r') as file:
+        params = yaml.safe_load(file)
+    return params
+
+
+
+
+
+if __name__ == "__main__":
+    yml_file_path = 'params.yml'
+    params = read_params_from_yml(yml_file_path)
+
+
+
+    wrap_images_all = registration_demo(params['image_dir'],params['demo_image_path'], params['json_ref_path'], params['ref_image_path'])
+    for i, item in enumerate(wrap_images_all):
+
+        wrap_image,wrap_images_name = item
+        similarity_results, same_content_boxes = compare_boxes_similarity(params['path_to_image1'], wrap_image, params['json_file_path'],
+                                                                          params['similarity_threshold'])
+        # 打印所有坐标框的相似度结果
+        print(f"{wrap_images_name}\n")
+        for idx, score in enumerate(similarity_results, 1):
+            print(f"Similarity Score for Box {idx}: {score}")
+
+        # 打印被认为是相同内容的坐标框
+        print("Boxes with the same content:")
+        for shape in same_content_boxes:
+            print(shape['label'] + ' object is same as template')

File diff suppressed because it is too large
+ 22 - 0
data/JSON/DSC_4604.json


File diff suppressed because it is too large
+ 30 - 0
data/JSON_MASK/DSC_4604_mask.json


BIN
data/QUERY/DSC_4575.JPG


BIN
data/QUERY/DSC_4576.JPG


BIN
data/QUERY/DSC_4577.JPG


BIN
data/QUERY/DSC_4579.JPG


BIN
data/QUERY/DSC_4581.JPG


BIN
data/QUERY/DSC_4582.JPG


BIN
data/QUERY/DSC_4585.JPG


BIN
data/QUERY/DSC_4586.JPG


BIN
data/QUERY/DSC_4587.JPG


BIN
data/QUERY/DSC_4588.JPG


BIN
data/QUERY/DSC_4590.JPG


BIN
data/QUERY/DSC_4591.JPG


BIN
data/QUERY/DSC_4594.JPG


BIN
data/QUERY/DSC_4596.JPG


BIN
data/QUERY/DSC_4597.JPG


BIN
data/QUERY/DSC_4599.JPG


BIN
data/QUERY/DSC_4600.JPG


BIN
data/QUERY/DSC_4602.JPG


BIN
data/QUERY/DSC_4603.JPG


BIN
data/REF/DSC_4604.JPG


BIN
data/demo_613/1_image_query/DSC_0474.JPG


BIN
data/demo_613/2_image_query/ADSC_0485.JPG


BIN
data/demo_613/2_image_query/DSC_0476.JPG


BIN
data/demo_613/2_image_query/DSC_0479.JPG


BIN
data/demo_613/2_image_query/DSC_0482.JPG


BIN
data/demo_613/2_image_query/DSC_0488.JPG


BIN
data/demo_613/2_image_query/DSC_0492.JPG


BIN
data/demo_613/3_image_query/ADSC_0480.JPG


BIN
data/demo_613/3_image_query/DSC_0477.JPG


BIN
data/demo_613/3_image_query/DSC_0483.JPG


BIN
data/demo_613/3_image_query/DSC_0486.JPG


BIN
data/demo_613/3_image_query/DSC_0489.JPG


BIN
data/demo_613/3_image_query/DSC_0493.JPG


File diff suppressed because it is too large
+ 262 - 0
data/demo_613/json/ADSC_0478.json


File diff suppressed because it is too large
+ 30 - 0
data/demo_613/json/ADSC_0478_mask.json


File diff suppressed because it is too large
+ 118 - 0
data/demo_613/json/ADSC_0480.json


File diff suppressed because it is too large
+ 30 - 0
data/demo_613/json/ADSC_0480_mask.json


File diff suppressed because it is too large
+ 182 - 0
data/demo_613/json/ADSC_0485.json


File diff suppressed because it is too large
+ 30 - 0
data/demo_613/json/ADSC_0485_mask.json


BIN
data/demo_613/ref_image/ADSC_0478.JPG


BIN
data/demo_613/ref_image/ADSC_0480.JPG


BIN
data/demo_613/ref_image/ADSC_0485.JPG


BIN
data/images/one_pillar_pagoda_1.jpg


BIN
data/images/one_pillar_pagoda_2.jpg


File diff suppressed because it is too large
+ 262 - 0
data/json/image165214-001.json


File diff suppressed because it is too large
+ 214 - 0
data/json/image165214-019.json


File diff suppressed because it is too large
+ 166 - 0
data/json/image165214-037.json


File diff suppressed because it is too large
+ 70 - 0
data/json1/DSC_0449.json


File diff suppressed because it is too large
+ 30 - 0
data/json1/DSC_0449_mask.json


File diff suppressed because it is too large
+ 70 - 0
data/json1/DSC_0453.json


File diff suppressed because it is too large
+ 30 - 0
data/json1/DSC_0453_mask.json


File diff suppressed because it is too large
+ 30 - 0
data/mask_json/image165214-001_mask.json


File diff suppressed because it is too large
+ 30 - 0
data/mask_json/image165214-019_mask.json


File diff suppressed because it is too large
+ 30 - 0
data/mask_json/image165214-037_mask.json


BIN
data/new/DSC_4531.JPG


BIN
data/new/DSC_4532.JPG


BIN
data/new/DSC_4533.JPG


BIN
data/new/DSC_4534.JPG


BIN
data/new/DSC_4535.JPG


BIN
data/new/DSC_4536.JPG


BIN
data/new/DSC_4537.JPG


BIN
data/new/DSC_4538.JPG


BIN
data/new/DSC_4539.JPG


BIN
data/new/DSC_4540.JPG


BIN
data/new/DSC_4541.JPG


BIN
data/new/DSC_4542.JPG


BIN
data/new/DSC_4543.JPG


BIN
data/new/DSC_4544.JPG


BIN
data/new/DSC_4545.JPG


BIN
data/new/DSC_4546.JPG


BIN
data/new/DSC_4547.JPG


BIN
data/new/DSC_4548.JPG


BIN
data/new/DSC_4549.JPG


BIN
data/new/DSC_4550.JPG


BIN
data/new/DSC_4551.JPG


BIN
data/new/DSC_4552.JPG


BIN
data/new_images/DSC_4527.JPG


BIN
data/new_images/DSC_4528.JPG


BIN
data/new_images/DSC_4529.JPG


BIN
data/new_images/DSC_4530.JPG


BIN
data/new_images/DSC_4531.JPG


BIN
data/new_images/DSC_4532.JPG


BIN
data/new_images/DSC_4533.JPG


BIN
data/new_images/DSC_4534.JPG


BIN
data/new_images/DSC_4535.JPG


BIN
data/new_images/DSC_4536.JPG


BIN
data/new_images/DSC_4537.JPG


BIN
data/new_images/DSC_4538.JPG


BIN
data/new_images/DSC_4539.JPG


BIN
data/new_images/DSC_4540.JPG


BIN
data/new_images/DSC_4541.JPG


BIN
data/new_images/DSC_4542.JPG


BIN
data/new_images/DSC_4543.JPG


BIN
data/new_images/DSC_4544.JPG


BIN
data/new_images/DSC_4545.JPG


BIN
data/new_images/DSC_4546.JPG


File diff suppressed because it is too large
+ 70 - 0
data/similarity_image/JSON_path/frame_000000.json


Some files were not shown because too many files changed in this diff