21BRS1203_rp_lab9_rcnn
21BRS1203_rp_lab9_rcnn
Prof Muthukumaran K
Vishvender Tyagi
21BRS1203
Lab RCNN Object Detection
Code
import cv2
import numpy as np
from sklearn.svm import SVC
from sklearn.preprocessing import StandardScaler
import pickle
import os
from google.colab.patches import cv2_imshow
def load_image_and_propose_regions(image_path):
# Check if the file exists
if not os.path.isfile(image_path):
raise FileNotFoundError(f"The image file at {image_path} was not
found.")
proposals = []
for (x, y, w, h) in rects[:100]: # Limit to 100 proposals
proposals.append((x, y, w, h, image[y:y + h, x:x + w])) # Append
the coordinates and patch
svm = SVC(probability=True)
svm.fit(X_train_scaled, y_train)
return results
def main():
# Load dataset
image_path = 'rcnn_image2.jpg' # Your input image path
image, proposals = load_image_and_propose_regions(image_path)
X_train = []
y_train = []
for i in range(len(proposals)):
features = extract_features(proposals[i][4])
X_train.append(features)
y_train.append(1 if i % 2 == 0 else 0) # Dummy labels
X_train = np.array(X_train)
y_train = np.array(y_train)
# Train SVM
svm, scaler = train_svm(X_train, y_train)
cv2_imshow(image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Image
Output