-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSurfaceDamageInference.py
executable file
·53 lines (47 loc) · 1.53 KB
/
SurfaceDamageInference.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
import torch
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import PIL
from PIL import Image
from PIL import ImageFile
import cv2
import imageio
ImageFile.LOAD_TRUNCATED_IMAGES = True
# Inference environment params.
model = torch.load("SurfaceDamageDetectionModel")
classes = {0:"HighDamage",1:"LowDamage",2:"MediumDamage",3:"NoDamage"}
def detect(frame,net,transform):
frame = PIL.Image.fromarray(frame)
print(frame.size)
return frame
# define pro-processing applied to images in a transformation object.
transform = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
video_capture = cv2.VideoCapture(1)
video_capture.open(1)
print(video_capture.isOpened())
while True:
_,frame = video_capture.read()
# pre-process the frame entered from camera feed.
img=cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
img=Image.fromarray(img)
img=transform(img)
img=np.asarray(img)
img=torch.from_numpy(img)
img=img.view(1,3,224,224)
# pass the image produced to the model and print the output of the predicted label.
model.eval()
y_ = model(img)
_, y_label_ = torch.max(y_, 1)
print(classes[y_label_.data.cpu().numpy()[0]])
cv2.imshow("frame", frame)
# provide a way for exit from the main loop program.
if cv2.waitKey(500) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()