-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwater_meter_app.py
126 lines (115 loc) · 4.44 KB
/
water_meter_app.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import pandas as pd
import numpy as np
import os, sys
# import seaborn as sns
import matplotlib.pyplot as plt
import glob
# from PIL import Image, ImageOps
# from plotly.subplots import make_subplots
# from warnings import filterwarnings
from io import StringIO, BytesIO
import streamlit as st
import torch
import cv2
# import argparse
from tempfile import NamedTemporaryFile
import warnings
from yolov5 import detect_yolov5
from PIL import Image
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--img", required=False, help="path to image")
# args = vars(ap.parse_args())
def load_image(img_path, resize=True):
img = cv2.cvtColor(cv2.imread(str(img_path)), cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (300, 300), interpolation = cv2.INTER_AREA)
# img = Image.open(img_path)
# img = ImageOps.exif_transpose(img.resize((300, 300), Image.ANTIALIAS))
return img
def show_grid(image_paths):
images = [load_image(img) for img in image_paths]
images = torch.as_tensor(images)
images = images.permute(0, 3, 1, 2)
# grid_img = torchvision.utils.make_grid(images, nrow=11)
plt.figure(figsize=(24, 12))
# plt.imshow(grid_img.permute(1, 2, 0))
plt.axis('off')
def format_predictions(img_path, results, num_classes=8):
"""
Format the predictions as they are not in order of the meter reading
For some reason inside this app, the detect script generates more rows than
it does when tested in Colab. So, I have taken the top num_classes records
"""
if not isinstance(results, str):
df = results.pandas().xyxy[0].head(num_classes)
else:
df = pd.read_csv(results, sep=' ', header=None)
df.columns = ['xmin', 'ymin', 'xmax', 'ymax', 'conf', 'class', 'name']
df.sort_values('xmin', ascending=True, inplace=True)
img = load_image(img_path)
fig = plt.figure(figsize=(7, 7))
plt.imshow(img)
print('df.values',df['class'].values)
reading = ''
for s in df['class'].values:
# 防止把仪表识别结果误加入读数中
if s != 10:
reading = reading + str(s)
# reading = ''.join(str(s) for s in df['class'].values)
# if len(reading) > 5:
# reading = reading[:-3] + '.' + reading[-3:]
# reading = reading[2:]
try:
reading = float(reading)
# plt.title(str(img_path) + "\n" + str(img) + "\n" + "Predict labels: " + str(df['class'].values) + "\n" + 'Reading: ' + str(reading) + " m\u00b3")
plt.title("Predict labels: " + str(df['class'].values) + "\n" + 'Reading: ' + str(reading) + " m\u00b3")
except:
plt.title('Is this a valid water meter image...?')
plt.axis('off')
st.pyplot(fig)
@st.cache
def load_model(src, path, device, reload=False):
return torch.hub.load(src, 'custom', path=path, device=device, force_reload=reload)
@st.cache
def detect(weights, source):
# os.system("python yolov5-master/detect.py --weights %s --source %s" % (weights, source))
# img = load_image(source)
im0 = detect_yolov5.run(weights, source)
fig = plt.figure(figsize=(7, 7))
plt.imshow(im0)
plt.axis('off')
st.pyplot(fig)
@st.cache(ttl=24*3600, suppress_st_warning=True, show_spinner=False)
def predict(inp):
# results = model(args['img'])
# format_predictions(args['img'], results)
if inp:
# https://discuss.streamlit.io/t/image-upload-problem/4810/5
temp_file = NamedTemporaryFile(delete=True)
temp_file.write(inp.getvalue())
col1, col2 = st.columns(2)
with col1:
st.header("Original picture")
results = model.to(device)(temp_file.name)
format_predictions(temp_file.name, results)
with col2:
# st.header("Predict picture")
st.header("Predict picture")
# results = model.to(device)(temp_file.name)
# format_predictions(temp_file.name, results)
weights = 'weights/best.pt'
# detect(weights, temp_file.name)
img = Image.open(inp)
detect(weights, inp)
if __name__ == '__main__':
yolo_path = os.path.join(os.getcwd(), 'yolov5')
# location = 'runs/train/yolov5x_water_meter/weights/best.pt'
location = 'weights/best.pt'
best_run = os.path.join(os.getcwd(), 'yolov5', location)
device = torch.cuda.get_device_properties(0).name if torch.cuda.is_available() else 'cpu'
# model = torch.hub.load('ultralytics/yolov5', 'custom', path=location, device=device, force_reload=False)
src = 'ultralytics/yolov5'
model = load_model(src, path=location, device=device)
warnings.filterwarnings('ignore')
st.title('Water meter reading system based on YOLOv5')
buffer = st.file_uploader("Upload image", type=['png', 'jpeg', 'jpg'])
predict(buffer)