File size: 1,164 Bytes
d4c9761
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 tensorflow as tf
import cv2
import numpy as np
from keras_preprocessing.image import img_to_array

# Get model
model = './saves/dogs-cats.h5'

IMAGE_SIZE = 100

CNN_MODEL = tf.keras.models.load_model(model)
weight_model = CNN_MODEL.get_weights()
CNN_MODEL.set_weights(weight_model)

# CAM
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    # Parse to gray
    img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    img = cv2.resize(img, (IMAGE_SIZE, IMAGE_SIZE), interpolation=cv2.INTER_CUBIC)

    # Img normalized
    img = np.array(img).astype(float) / 255

    # Parse to 2D array
    image = img_to_array(img)
    image = np.expand_dims(image, axis=0)

    # Predict
    prediction = CNN_MODEL.predict(image)
    prediction = prediction[0][0]

    print(prediction)

    # Classification
    if prediction <= 0.5:
        cv2.putText(frame, "Gato", (200, 70), cv2.FONT_HERSHEY_SIMPLEX, 3, (236, 19, 180), 6)
    else:
        cv2.putText(frame, "Perro", (200, 70), cv2.FONT_HERSHEY_SIMPLEX, 3, (20, 106, 231), 6)

    cv2.imshow("CNN", frame)

    t = cv2.waitKey(1)
    if t == 25:
        break

cv2.destroyAllWindows()
cap.release()