0% found this document useful (0 votes)
10 views4 pages

21BRS1203_rp_lab9_rcnn

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views4 pages

21BRS1203_rp_lab9_rcnn

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Robot Perception

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

# Placeholder function to simulate feature extraction


def extract_features(image):
if isinstance(image, np.ndarray):
return image.flatten()[:100]
else:
raise ValueError("Input to extract_features must be a numpy array
representing an image.")

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.")

# Load the image


image = cv2.imread(image_path)
if image is None:
raise ValueError(f"Failed to load image from {image_path}. Please
check the file format and path.")

height, width = image.shape[:2]

# Generate region proposals (using Selective Search)


ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()
ss.setBaseImage(image)
ss.switchToSelectiveSearchFast()
rects = ss.process()

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

return image, proposals

# Train SVM model


def train_svm(X_train, y_train):
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)

svm = SVC(probability=True)
svm.fit(X_train_scaled, y_train)

return svm, scaler

def predict(svm, scaler, proposals):


results = []
for proposal in proposals:
features = extract_features(proposal[4]) # Use the extracted
image patch
features_scaled = scaler.transform([features])
prob = svm.predict_proba(features_scaled)
results.append(prob[0]) # Get probabilities for classes

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)

# Predict on new proposals


predictions = predict(svm, scaler, proposals)

# Draw predictions on image


for i, (x, y, w, h, _) in enumerate(proposals):
if predictions[i][1] > 0.5: # Assuming class 1 is the positive
class
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.putText(image, f"Class 1: {predictions[i][1]:.2f}", (x, y
- 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

cv2_imshow(image)
cv2.waitKey(0)
cv2.destroyAllWindows()

if __name__ == "__main__":
main()

Image
Output

You might also like