Skip to content

Commit

Permalink
Create computer_vision.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored May 11, 2024
1 parent 918e5b0 commit 422c56b
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions models/ai_models/computer_vision.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Import necessary libraries
import cv2
import numpy as np
from sklearn.svm import SVC

# Define a class for computer vision models
class ComputerVision:
def __init__(self, model_type):
self.model_type = model_type
self.model = self._create_model()

def _create_model(self):
# Create a computer vision model using OpenCV and scikit-learn
if self.model_type == 'face_detection':
return cv2.dnn.readNetFromCaffe('face_detection_model.prototxt', 'face_detection_model.caffemodel')
elif self.model_type == 'object_detection':
return cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
elif self.model_type == 'image_classification':
return SVC(kernel='linear', probability=True)

def detect_faces(self, image):
# Detect faces in an image using the face detection model
blob = cv2.dnn.blobFromImage(image, 1, (300, 300), (104, 177, 123))
self.model.setInput(blob)
detections = self.model.forward()
faces = []
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > 0.5:
x1 = int(detections[0, 0, i, 3] * image.shape[1])
y1 = int(detections[0, 0, i, 4] * image.shape[0])
x2 = int(detections[0, 0, i, 5] * image.shape[1])
y2 = int(detections[0, 0, i, 6] * image.shape[0])
faces.append((x1, y1, x2, y2))
return faces

def detect_objects(self, image):
# Detect objects in an image using the object detection model
blob = cv2.dnn.blobFromImage(image, 1/255, (416, 416), [0,0,0], 1, crop=False)
self.model.setInput(blob)
outputs = self.model.forward(self.model.getUnconnectedOutLayersNames())
objects = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5:
x1 = int(detection[0] * image.shape[1])
y1 = int(detection[1] * image.shape[0])
x2 = int(detection[2] * image.shape[1])
y2 = int(detection[3] * image.shape[0])
objects.append((x1, y1,x2, y2, class_id, confidence))
return objects

def classify_image(self, image):
# Classify an image using the image classification model
# Implement feature extraction and classification logic here
pass

# Define a function to load a computer vision model
def load_computer_vision(model_path):
# Load the computer vision model from a file
model = ComputerVision('face_detection')
model.model = cv2.dnn.readNetFromCaffe(model_path)
return model

0 comments on commit 422c56b

Please sign in to comment.