letrunglinh commited on
Commit
c419dfc
·
1 Parent(s): 072aaec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import cv2
3
+ from model_fasterrcnn import LDRNet_fasterrcnn
4
+ import torch
5
+ import numpy as np
6
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
+
8
+ model = LDRNet_fasterrcnn()
9
+ model.load_state_dict(torch.load("last_0603_92.pth", map_location=device))
10
+ model.eval()
11
+ inputs = [
12
+ gr.Image(type="filepath", label="Input Image"),]
13
+ outputs = gr.Image(type="filepath", label="Output Image")
14
+ def PerspectiveTransform(img,coord):
15
+ # img = cv2.resize(img, (224, 224))
16
+ height,width,_ = img.shape
17
+ src_points = np.float32([[coord[0], coord[1]],[coord[2], coord[3]],[coord[4], coord[5]],[coord[6], coord[7]]])
18
+ dst_points = np.float32([[0, 0],[width, 0],[width, height],[0, height]])
19
+ transformation_matrix = cv2.getPerspectiveTransform(src_points, dst_points)
20
+ transformed_img = cv2.warpPerspective(img, transformation_matrix, (width, height))
21
+ return transformed_img
22
+ def detect(image):
23
+ image = cv2.imread(image)
24
+ original_size = image.shape[:2]
25
+ img = cv2.resize(image, (224, 224))
26
+ img_show = np.copy(image)
27
+ img = torch.tensor(img.astype(np.float32)).permute(2, 0, 1).unsqueeze(0)
28
+ img = (img - 0.5 * 255) / (0.5 * 255)
29
+ result = model(img)
30
+ results = result[0].detach().numpy()[0]
31
+ coord = results[0:8]
32
+ lines = result[1].detach().numpy()[0]
33
+
34
+ try:
35
+ coord = [int(x * 224) for x in coord]
36
+ lines = [int(x * 224) for x in lines]
37
+ img_show = cv2.cvtColor(img_show, cv2.COLOR_BGR2RGB)
38
+ coord = np.array(coord,dtype=np.int32)
39
+ coord = coord.reshape(4,2)
40
+ coord = np.floor(coord / np.array([224/original_size[1], 224/original_size[0]])).astype(np.int32)
41
+ coord = coord.reshape(1,8)[0]
42
+ origin = PerspectiveTransform(img_show,coord)
43
+ except Exception as e: print(e)
44
+
45
+ return origin
46
+ title = "Scan Documment 📄"
47
+ demo_app = gr.Interface(
48
+ fn=detect,
49
+ inputs=inputs,
50
+ outputs=outputs,
51
+ )
52
+ demo_app.launch(debug=True)