Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import load_model
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
data = pd.read_csv('species_info.csv')
|
| 8 |
+
|
| 9 |
+
def format_label(label):
|
| 10 |
+
"""
|
| 11 |
+
From '0 rùa khác\n' to 'rùa khác'
|
| 12 |
+
"""
|
| 13 |
+
return label[label.find(" ")+1:-1]
|
| 14 |
+
|
| 15 |
+
def check_species_status(species_name):
|
| 16 |
+
status = ''
|
| 17 |
+
|
| 18 |
+
return status
|
| 19 |
+
|
| 20 |
+
def info(species_name):
|
| 21 |
+
status = check_species_status(species_name)
|
| 22 |
+
if status == '':
|
| 23 |
+
info = ''
|
| 24 |
+
return info
|
| 25 |
+
|
| 26 |
+
def get_vi_name(en_name):
|
| 27 |
+
"""
|
| 28 |
+
Return name in Vietnamese
|
| 29 |
+
"""
|
| 30 |
+
print(en_name)
|
| 31 |
+
return data[data['en_name'] == en_name]['vi_name'].to_list()[0]
|
| 32 |
+
|
| 33 |
+
def get_law(en_name):
|
| 34 |
+
cites = data[data['en_name'] == en_name]['CITES'].to_list()[0]
|
| 35 |
+
nd06 = data[data['en_name'] == en_name]['ND06'].to_list()[0]
|
| 36 |
+
return cites, nd06
|
| 37 |
+
|
| 38 |
+
def get_habitat(en_name):
|
| 39 |
+
return data[data['en_name'] == en_name]['habitat'].to_list()[0]
|
| 40 |
+
|
| 41 |
+
def predict(image):
|
| 42 |
+
|
| 43 |
+
# Load the model
|
| 44 |
+
model = load_model('keras_model.h5')
|
| 45 |
+
|
| 46 |
+
# Create the array of the right shape to feed into the keras model
|
| 47 |
+
# The 'length' or number of images you can put into the array is
|
| 48 |
+
# determined by the first position in the shape tuple, in this case 1.
|
| 49 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 50 |
+
|
| 51 |
+
#resize the image to a 224x224 with the same strategy as in TM2:
|
| 52 |
+
#resizing the image to be at least 224x224 and then cropping from the center
|
| 53 |
+
size = (224, 224)
|
| 54 |
+
image = ImageOps.fit(image, size, Image.ANTIALIAS)
|
| 55 |
+
|
| 56 |
+
#turn the image into a numpy array
|
| 57 |
+
image_array = np.asarray(image)
|
| 58 |
+
# Normalize the image
|
| 59 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 60 |
+
# Load the image into the array
|
| 61 |
+
data[0] = normalized_image_array
|
| 62 |
+
|
| 63 |
+
# run the inference
|
| 64 |
+
pred = model.predict(data)
|
| 65 |
+
pred = pred.tolist()
|
| 66 |
+
|
| 67 |
+
with open('labels.txt','r') as f:
|
| 68 |
+
labels = f.readlines()
|
| 69 |
+
|
| 70 |
+
en_name = format_label(labels[pred.index(max(pred))]).strip()
|
| 71 |
+
|
| 72 |
+
result = {get_vi_name(format_label(labels[i])): round(pred[0][i],2) for i in range(len(pred[0]))}
|
| 73 |
+
cites, nd06 = get_law(en_name)
|
| 74 |
+
info = ""
|
| 75 |
+
if str(nd06) != "":
|
| 76 |
+
info += f'CITES: {cites}, NĐ06: {nd06} \n \n'
|
| 77 |
+
info += "Đây là loài được pháp luật bảo vệ. Mọi hành vi buôn bán, nuôi nhốt không có \
|
| 78 |
+
[giấy phép](https://thuvienphapluat.vn/van-ban/Tai-nguyen-Moi-truong/Nghi-dinh-06-2019-ND-CP-quan-ly-thuc-vat-rung-dong-vat-rung-nguy-cap-quy-hiem-405883.aspx) đều vi phạm pháp luật \n"
|
| 79 |
+
info += "- Nếu bạn vô tình bắt gặp loài này bị buôn bán mà không có giấy phép, \
|
| 80 |
+
tuyệt đối không mua nhằm bất kỳ mục đích gì (ví dụ để phóng sinh) \
|
| 81 |
+
mà nên báo cáo vi phạm tại đường dây nóng bảo vệ DVHD của ENV **1800-1522**. \n"
|
| 82 |
+
info += "- Nếu bạn đang nuôi thì nên giao nộp cho cơ quan chức năng để trả về tự nhiên. Tham khảo đơn vị tiếp nhận DVHD ở địa phương \
|
| 83 |
+
bạn tại [đây](https://drive.google.com/file/d/1K2ZWcHKGEsNudh_LtHgHJOXlVw-GQ6AZ/view). \n"
|
| 84 |
+
info += f"- Nếu bạn bắt gặp trong vườn nhà thì có thể xem xét thả chúng về môi trường sống. Hãy đảm bảo nơi bạn thả là\
|
| 85 |
+
**{get_habitat(en_name)}**."
|
| 86 |
+
|
| 87 |
+
return result, info
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
description="""
|
| 91 |
+
VNTurtle nhận diện các loài rùa Việt Nam. Mô hình mẫu này có thể nhận diện 10 loại rùa thường xuất hiện ở VN gồm **5** loài bản địa
|
| 92 |
+
**(1) Rùa núi viền**, **(2) Rùa núi vàng**, **(3) Rùa ba gờ**, **(4) Rùa răng**, và **(5) Rùa sa nhân**,
|
| 93 |
+
và **5** loài ngoại lai **(1) Rùa Sulcata**, **(2) Rùa bản đồ**, **(3) Rùa cá sấu**, **(4) Rùa tai đỏ**, và **(5) Rùa ninja**
|
| 94 |
+
"""
|
| 95 |
+
article = "© Hình ảnh minh hoạ được cung cấp bởi ATP"
|
| 96 |
+
examples = [ ['test1.jpg'], ['test2.jpg'], ['test3.jpg'] ]
|
| 97 |
+
gr.Interface(fn=predict,
|
| 98 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 99 |
+
outputs=[gr.Label(label="Predictions"), gr.Markdown()],
|
| 100 |
+
live=True,
|
| 101 |
+
title='VNTurtle - Toy Model',
|
| 102 |
+
description=description,
|
| 103 |
+
examples=examples,
|
| 104 |
+
article=article).launch()
|