-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_shot.py
executable file
·61 lines (53 loc) · 1.79 KB
/
predict_shot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import subprocess
import json
import sys
import os
import random
import string
import shutil
import requests
from pathlib import Path
def find_docker_container(container_name, call_count=0):
try:
output = subprocess.check_output(["docker", "ps", "-q", "-f", f"name={container_name}"])
results = output.decode('utf-8').strip()
if results:
return results
except subprocess.CalledProcessError:
pass
print("Cannot find Docker container. Starting a new one.")
subprocess.check_call("sh run_docker.sh".split())
if call_count == 0:
return find_docker_container(container_name, call_count=1)
else:
return None
def inference(image):
url = "http://localhost:3100/predict/"
response = requests.post(url, files={"file": ("tmp.jpg", image, "image/jpeg")})
if response.status_code == 200:
return json.loads(response.json())
else:
print(f"Failed to get a response, status code: {response.status_code}")
return None
def predict(image):
random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=10))
container_name = "mito_detector"
results_file_path = f"results_{random_string}.json"
container_id = find_docker_container(container_name)
if not container_id:
print("Docker container not found.")
sys.exit(1)
# copy image_path to ./workspace not using docker
# run_server_in_docker(container_id)
results = inference(image)
return results
# debug
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: python predict_shot.py <path_to_image>")
sys.exit(1)
image_path = sys.argv[1]
with open(image_path, 'rb') as file:
image = file.read()
results = predict(image)
print("DEBUG:",results)