Skip to content

Commit 0605ca0

Browse files
committed
Script for prediction
1 parent a12e1cd commit 0605ca0

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

trash_classifier.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import keras
2+
from pypylon import pylon
3+
from keras.models import Model, load_model
4+
from keras.applications import mobilenet
5+
from keras.applications.mobilenet import preprocess_input
6+
from keras.preprocessing import image
7+
from keras.utils.generic_utils import CustomObjectScope
8+
import numpy as np
9+
import matplotlib.pyplot as plt
10+
import time
11+
import cv2
12+
13+
14+
def pp_image(img):
15+
img = image.load_img('pic.png', target_size=(224, 224))
16+
x = image.img_to_array(img)
17+
x = np.expand_dims(x, axis=0)
18+
x = preprocess_input(x)
19+
20+
return np.asarray(x)
21+
22+
23+
model=load_model('models/model1.h5', custom_objects={'relu6': mobilenet.relu6})
24+
25+
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
26+
27+
numberOfImagesToGrab = 100
28+
camera.StartGrabbingMax(numberOfImagesToGrab)
29+
30+
converter = pylon.ImageFormatConverter()
31+
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
32+
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned
33+
34+
while camera.IsGrabbing():
35+
time.sleep(0.005)
36+
grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
37+
38+
if grabResult.GrabSucceeded():
39+
# Access the image data.
40+
print("SizeX: ", grabResult.Width)
41+
print("SizeY: ", grabResult.Height)
42+
import ipdb; ipdb.set_trace()
43+
img = converter.Convert(grabResult).GetArray()
44+
cv2.imwrite('pic.png', img)
45+
img=pp_image(img)
46+
yo=model.predict(img)
47+
print("Gray value of first pixel: ", img[0, 0])
48+
49+
grabResult.Release()

0 commit comments

Comments
 (0)