You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

store_result.py 3.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2021 The KubeEdge Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from pathlib import Path
  15. import os
  16. import cv2
  17. from PIL import Image
  18. from sedna.common.log import LOGGER
  19. from sedna.core.multi_edge_inference.data_classes import DetTrackResult
  20. from sedna.algorithms.reid.close_contact_estimation import ContactTracker
  21. def create_results_folder(folder):
  22. """Creates result folder if it doesn't exist"""
  23. Path(folder).mkdir(parents=False, exist_ok=True)
  24. def write_text_on_image(img, text, textX, textY, color=(0, 255, 0)):
  25. font = cv2.FONT_HERSHEY_SIMPLEX
  26. height, width, _ = img.shape
  27. tl = round(0.002 * (height + width) * 0.1) + 1
  28. # add text centered on image
  29. cv2.putText(img, text, (textX, textY), font, 0.5, color, tl)
  30. def draw_bbox(image, bbox, text="CARRIER", color=(255,0,0), tracking_id=None):
  31. height, width, _ = image.shape
  32. tl = round(0.002 * (height + width) * 0.1) + 1 # line thickness
  33. target_centroid = (int((bbox[0]+bbox[2])/2), int((bbox[1]+bbox[3])/2))
  34. write_text_on_image(image, text, int(bbox[0]), int(bbox[1])-10, color)
  35. if tracking_id:
  36. write_text_on_image(image, str(tracking_id), int(bbox[0]), int(bbox[3])-10, color)
  37. cv2.rectangle(image, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), color, tl)
  38. cv2.circle(image, target_centroid, radius=3, color=color, thickness=tl)
  39. return target_centroid
  40. def save_image(data : DetTrackResult, CT : ContactTracker, folder):
  41. image_name = data.image_key.rsplit("_")[0] + ".png"
  42. image = cv2.imdecode(data.scene, cv2.IMREAD_COLOR)
  43. # We need the original image to avoid drawing artifacts when performing some operations
  44. subjects_at_risk = [data.targetID[data.is_target]]
  45. # TARGET
  46. idx = data.is_target
  47. target_centroid = draw_bbox(image, data.bbox_coord[idx], tracking_id=data.tracklets[idx])
  48. CT.prep_homography(image.shape, data.bbox_coord[idx])
  49. # NON-TARGETS
  50. for lidx, bbox in enumerate(data.bbox_coord):
  51. # Draw bounding box
  52. if lidx != idx:
  53. other_centroid = (int((bbox[0]+bbox[2])/2), int((bbox[1]+bbox[3])/2))
  54. in_risk = CT.in_risk_zone(image, bbox)
  55. if in_risk:
  56. draw_bbox(image, bbox, "P-RISK", (255,69,0), tracking_id=data.tracklets[lidx])
  57. cv2.line(image, target_centroid, other_centroid, (0, 255, 0), thickness=1)
  58. cv2.circle(image, other_centroid, radius=3, color=(255, 0, 0), thickness=1)
  59. subjects_at_risk.append(data.targetID[lidx])
  60. # Add camera
  61. write_text_on_image(image, f"Camera:{data.camera}", 0, 30)
  62. # Save result in the specified folder
  63. create_results_folder(folder)
  64. base_image = Image.fromarray(image).resize((640, 480))
  65. base_image.save(os.path.join(folder, image_name), quality=80, optimize=True)
  66. return image_name