File size: 2,075 Bytes
b6aa8ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import cv2
import numpy as np
from tensorflow.keras.models import load_model

# 🔹 Load trained model
model = load_model("my_image_model.h5")

# 🔹 Your class labels
class_indices = {"Gir_cow": 0, "Murrah_buffalo": 1, "Red_Sindhi_cow": 2, "Sahiwal_cow": 3, "Tharparkar_cow": 4, "amritmahal_cow": 5, "banni_buffalo": 6, "bhadwari_buffalo": 7, "dharwadi_buffalo": 8, "jafarabadi_buffalo": 9}# Update with your classes

# 🔹 Preprocessing function
def preprocess_frame(frame, target_size=(224, 224)):
    img = cv2.resize(frame, target_size)          # Resize
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)    # BGR -> RGB
    img = img.astype("float32") / 255.0           # Normalize
    img = np.expand_dims(img, axis=0)             # Add batch dimension
    return img

# 🔹 Start webcam
cap = cv2.VideoCapture(0)

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

    h, w, _ = frame.shape

    # Define square region (centered)
    size = min(h, w) // 2
    x1 = w // 2 - size // 2
    y1 = h // 2 - size // 2
    x2 = w // 2 + size // 2
    y2 = h // 2 + size // 2

    # Draw square
    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)

    # Extract ROI inside square
    roi = frame[y1:y2, x1:x2]

    # Preprocess ROI
    processed = preprocess_frame(roi, target_size=(224, 224))

    # Predict
    preds = model.predict(processed)
    class_id = np.argmax(preds, axis=1)[0]
    confidence = np.max(preds)

    # Label
    label = class_indices.get(class_id, str(class_id))
    text = f"{label} ({confidence:.2f})"

    # Put label inside square (centered)
    text_size = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.9, 2)[0]
    text_x = x1 + (size - text_size[0]) // 2
    text_y = y1 + (size + text_size[1]) // 2

    cv2.putText(frame, text, (text_x, text_y),
                cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2, cv2.LINE_AA)

    # Show frame
    cv2.imshow("Detection with Square ROI", frame)

    # Quit with 'q'
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()