forked from stipa01/passion_fruit_mxnet_mobile_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
detect.py
55 lines (44 loc) · 1.63 KB
/
detect.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
import datetime
import threading
import time
from kivy.graphics.texture import Texture
import requests
import numpy as np
import cv2
import json
import os
import glob
from kivymd.toast import toast
class DetectFruitHealth:
def __init__(self):
self.image = None
self.output_image_path = None
files = glob.glob('inference_outputs/*')
for f in files:
os.remove(f)
def infer(self, img_path):
url = 'https://zendimak-passion-image.herokuapp.com/predict'
payload = {"file": open(img_path, "rb")}
start_time = time.time()
response = requests.post(url=url, files=payload)
if response.status_code == 200:
image = np.array(json.loads(response.text).get('results'))
img_path = 'inference_outputs/output{:}.jpg'.format(datetime.datetime.now())
cv2.imwrite(img_path, image)
self.output_image_path = img_path
toast('Inference took: {:.2f} ms'.format((time.time() - start_time)*1000))
self.detecting = False
else:
self.detecting = False
toast('Check your internet connection')
def post_process(self):
buf = cv2.flip(self.image, 0).tostring()
texture = Texture.create(size=(self.image.shape[1], self.image.shape[0]), colorfmt='bgr')
texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
return texture
def invoke(self, img_path=None):
self.infer(img_path)
def run(self, img_path):
threading.Thread(target=self.invoke, args=(img_path,), daemon=True).start()
if __name__ == '__main':
obj = DetectFruitHealth()