reqllm.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import cv2
  2. import base64
  3. import threading
  4. import requests
  5. prompt_words = {
  6. "phone" : "1",
  7. "fall" : "2",
  8. "fire" : "3"
  9. }
  10. def cv2_to_base64(image):
  11. image1 = cv2.imencode('.jpg', image)[1]
  12. image_code = str(base64.b64encode(image1))[2:-1]
  13. return image_code
  14. def get_cut_position(boxes, w, h):
  15. xmin, ymin, xmax, ymax = w, h, 0, 0
  16. if len(boxes) == 1:
  17. xmin, ymin, xmax, ymax = boxes[0]
  18. box_w = xmax - xmin
  19. box_h = ymax - ymin
  20. xmin = max(0, xmin - box_w // 5)
  21. ymin = max(0, ymin - box_h // 5)
  22. xmax = min(w, xmax + box_w // 5)
  23. ymax = max(h, ymax + box_h // 5)
  24. else:
  25. for box in boxes:
  26. x1, y1, x2, y2 = box
  27. xmin = min(x1, xmin)
  28. ymin = min(y1, ymin)
  29. xmax = max(xmax, x2)
  30. ymax = max(ymax, y2)
  31. box_w = xmax - xmin
  32. box_h = ymax - ymin
  33. xmin = max(0, xmin - box_w // 2)
  34. ymin = max(0, ymin - box_h // 2)
  35. xmax = min(w, xmax + box_w // 2)
  36. ymax = max(h, ymax + box_h // 2)
  37. return xmin, ymin, xmax, ymax
  38. def post_illegal_data():
  39. pass
  40. def task(boxes, image, task_name, llm_url):
  41. """
  42. boxes : 违章框的列表 [[x1,y1,x2,y2], ...]
  43. image : 图片的opencv格式
  44. task_name : 任务名称 phone : 打电话, fall : 摔倒, fire : 烟雾火焰
  45. """
  46. h, w, _ = image.shape
  47. xmin, ymin, xmax, ymax = get_cut_position(boxes, w, h)
  48. crop_image = image[ymin:ymax, xmin:xmax]
  49. crop_image_base64 = cv2_to_base64(crop_image)
  50. prompt_word = prompt_words[task_name]
  51. data = {
  52. "imageData" : crop_image_base64,
  53. "text" : prompt_word
  54. }
  55. try:
  56. response = requests.post(llm_url, json=data)
  57. except Exception as error:
  58. print(error)
  59. return
  60. res = response.json()
  61. if res["data"]["illegal"] == 1:
  62. post_illegal_data()
  63. def create_llm_task(boxes, image, task_name, llm_url):
  64. llm_thread = threading.Thread(target=task, args=(boxes, image, task_name, llm_url), daemon=True)
  65. llm_thread.start()