0% found this document useful (0 votes)
20 views3 pages

Exp-6 Mechanical

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 3

6.

Data labelling for various images using object recognition in


python

Data labeling is an important task in machine learning, especially in the field of


computer vision where object recognition plays a crucial role. In Python, you can use
various libraries and tools to label images using object recognition, such as OpenCV,
TensorFlow, and Keras.

Here is a general process you can follow to label images using object recognition in
Python:

 Collect and prepare your data: Gather a dataset of images that you want to
label. You can use online resources like ImageNet, COCO, or create your own
dataset. Ensure that the images are of good quality and contain the objects you
want to detect. You can use OpenCV, Pillow, or scikit-image to preprocess the
images.
 Train a model: You can use TensorFlow or Keras to train an object detection
model using a pre-trained model like YOLOv3, SSD, or Faster R-CNN. You
can fine-tune the model on your dataset to improve its accuracy.
 Use the model to label images: Once the model is trained, you can use it to
label images automatically. You can use the OpenCV library to load an image
and detect objects in it. Then you can draw bounding boxes around the
detected objects and label them accordingly.

Here is an example of how to label images using object recognition in Python using
the TensorFlow Object Detection API:
Program:

1. Install the TensorFlow Object Detection API:

!pip installtensorflow

!pip installtensorflow-object-detection-api

2. Download the pre-trained model and sample images:


!wgethttp://download.tensorflow.org/models/object_detection/tf2/20210329/
efficientdet_d0_coco17_tpu-32.tar.gz

!tar -xf efficientdet_d0_coco17_tpu-32.tar.gz

!wgethttps://github.com/tensorflow/models/raw/master/research/object_detection/
test_images/ducky.jpg

!wgethttps://github.com/tensorflow/models/raw/master/research/object_detection/
test_images/image1.jpg

3. Load the model and detect objects in the images:


Import tensorflow as tf
Import os
import cv2
import numpy as np

# Load the pre-trained model


model_dir = 'efficientdet_d0_coco17_tpu-32/saved_model'
model = tf.saved_model.load(model_dir)

# Load the images


image_paths = ['ducky.jpg', 'image1.jpg']
images = []
for image_path in image_paths:
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
images.append(image)

# Detect objects in the images


for i, image in enumerate(images):
input_tensor = tf.convert_to_tensor(image)
input_tensor = input_tensor[tf.newaxis, ...]
detections = model(input_tensor)
boxes = detections['detection_boxes'][0].numpy()
classes = detections['detection_classes'][0].numpy().astype(np.uint32)
scores = detections['detection_scores'][0].numpy()

# Draw bounding boxes around the detected objects


for j in range(boxes.shape[0]):
if scores[j] > 0.5:
box = boxes[j] * np.array([image.shape[0], image.shape[1], image.shape[0],
image.shape[1]])
box = box.astype(np.int32)
cv2.rectangle(image, (box[1], box[0]), (box[3], box[2]), (0, 255, 0), 2)
cv2.putText(image, f'Class: {classes[j]}, Score: {scores[j]:.2f}', (box[1], box[0]),
cv2.FONT_HERSHEY_SIMPLEX

You might also like