Repository navigation
Expand file tree
/
Copy pathComputer-Vision.py
More file actions
118 lines (96 loc) · 3.71 KB
/
Copy pathComputer-Vision.py
File metadata and controls
118 lines (96 loc) · 3.71 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# computer_vision.py
"""
Computer Vision Module for Autonomous Vehicle Navigation
This module contains the implementation of computer vision techniques
to detect and classify objects in the vehicle's environment.
Techniques Used:
- Convolutional Neural Networks (CNNs)
- Object Detection
- Semantic Segmentation
"""
import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
class ComputerVision:
def __init__(self, model_path):
"""
Initialize the ComputerVision class with the path to the trained model.
:param model_path: str, path to the pre-trained model
"""
self.model = load_model(model_path)
def preprocess_image(self, img_path, target_size=(224, 224)):
"""
Preprocess the input image for prediction.
:param img_path: str, path to the input image
:param target_size: tuple, target size for resizing the image
:return: preprocessed image array
"""
img = image.load_img(img_path, target_size=target_size)
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
return img_array
def predict(self, img_array):
"""
Make a prediction on the input image.
:param img_array: array, preprocessed image array
:return: prediction result
"""
predictions = self.model.predict(img_array)
return predictions
def object_detection(self, frame):
"""
Perform object detection on the input frame.
:param frame: array, input frame from the camera
:return: frame with detected objects
"""
# Placeholder for object detection implementation
# Replace with your object detection logic
return frame
def semantic_segmentation(self, frame):
"""
Perform semantic segmentation on the input frame.
:param frame: array, input frame from the camera
:return: segmented frame
"""
# Placeholder for semantic segmentation implementation
# Replace with your semantic segmentation logic
return frame
def process_video(self, video_path, output_path):
"""
Process a video file for object detection and segmentation.
:param video_path: str, path to the input video file
:param output_path: str, path to save the output video file
"""
cap = cv2.VideoCapture(video_path)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(output_path, fourcc, 20.0, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if ret:
detected_frame = self.object_detection(frame)
segmented_frame = self.semantic_segmentation(detected_frame)
out.write(segmented_frame)
cv2.imshow('Frame', segmented_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
# Example usage
model_path = 'models/object_detection_model.h5'
cv_module = ComputerVision(model_path)
# Process a sample image
img_path = 'data/sample_image.jpg'
img_array = cv_module.preprocess_image(img_path)
predictions = cv_module.predict(img_array)
print("Predictions:", predictions)
# Process a sample video
video_path = 'data/sample_video.mp4'
output_path = 'data/output_video.avi'
cv_module.process_video(video_path, output_path)