Advertisement
kopyl

Untitled

Jul 5th, 2023
947
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1.  
  2. from deepsparse import Pipeline
  3. import time
  4. import cv2
  5. import numpy as np
  6. import urllib.request
  7.  
  8. model_path = "model.onnx"
  9. yolo_pipeline = Pipeline.create(
  10.     task="yolov8",
  11.     model_path=model_path,
  12. )
  13.  
  14. def resize_with_pad(image,
  15.                     new_shape,
  16.                     padding_color = (128, 128, 128)):
  17.     """
  18.    https://gist.github.com/IdeaKing/11cf5e146d23c5bb219ba3508cca89ec
  19.    """
  20.     original_shape = (image.shape[1], image.shape[0])
  21.     ratio = float(max(new_shape))/max(original_shape)
  22.     new_size = tuple([int(x*ratio) for x in original_shape])
  23.     image = cv2.resize(image, new_size)
  24.     delta_w = new_shape[0] - new_size[0]
  25.     delta_h = new_shape[1] - new_size[1]
  26.     top, bottom = delta_h//2, delta_h-(delta_h//2)
  27.     left, right = delta_w//2, delta_w-(delta_w//2)
  28.     image = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=padding_color)
  29.     # save padded image
  30.     return image
  31.  
  32. def get_image_from_url(image_url):
  33.     try:
  34.         req = urllib.request.Request(image_url, headers={"User-Agent": "Mozilla/5.0"})
  35.         with urllib.request.urlopen(req) as url:
  36.             image_contents = url.read()
  37.     except urllib.error.HTTPError as e:
  38.         return {"error": str(e)}
  39.     except ConnectionResetError:
  40.         return {"error": "ConnectionResetError"}
  41.     arr = np.asarray(bytearray(image_contents), dtype=np.uint8)
  42.     img = cv2.imdecode(arr, -1)
  43.     return {"image": img}
  44.  
  45.  
  46. image_url = "https://cdn.discordapp.com/attachments/1083610420096532581/1125730189733810239/padded.png"
  47. img = get_image_from_url(image_url)
  48. img = img["image"]
  49. img = resize_with_pad(img, (800, 800))
  50. input_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  51. pipeline_outputs = yolo_pipeline(images=[input_img], conf_thres=0.35, iou_thres=0.6)
  52. print(pipeline_outputs)
  53.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement