0% found this document useful (0 votes)
13 views6 pages

CV Task

Uploaded by

GOKUL M P
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)
13 views6 pages

CV Task

Uploaded by

GOKUL M P
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/ 6

CV task

Task-5

Classify the vehicles on the road and calculate the total count of vehicles moving along the roadway
in the traffic monitoring video

pip install opencv-python-headless tensorflow numpy

import cv2

import numpy as np

# Load YOLO

net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")

layer_names = net.getLayerNames()

output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

# Load the COCO class labels YOLO model was trained on

classes = open("coco.names").read().strip().split("\n")

vehicle_classes = ["car", "bus", "truck", "motorbike", "bicycle"]

# Load video

cap = cv2.VideoCapture('traffic_video.mp4')

while cap.isOpened():

ret, frame = cap.read()

if not ret:

break

height, width = frame.shape[:2]

blob = cv2.dnn.blobFromImage(frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)

net.setInput(blob)

outs = net.forward(output_layers)
vehicle_count = 0

for out in outs:

for detection in out:

scores = detection[5:]

class_id = np.argmax(scores)

confidence = scores[class_id]

if confidence > 0.5:

center_x, center_y, w, h = (detection[0:4] * [width, height, width, height]).astype(int)

x, y = center_x - w // 2, center_y - h // 2

label = str(classes[class_id])

if label in vehicle_classes:

vehicle_count += 1

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

cv2.putText(frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

cv2.putText(frame, f"Vehicle Count: {vehicle_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,


(255, 0, 0), 2)

cv2.imshow('Traffic Monitoring', frame)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

cv2.destroyAllWindows()

__________________________________________________________________________________

Task-6

Apply image segmentation to identify and locate the people present in the Video Surveillance

import cv2

import numpy as np

import tensorflow as tf
# Load the DeepLabV3 model from TensorFlow Hub

model = tf.saved_model.load("https://tfhub.dev/tensorflow/deeplabv3/1")

# Function to run the model and get segmentation results

def run_model(image):

image = tf.image.resize(image, [513, 513])

image = tf.cast(image, tf.uint8)

result = model(image)

return result['semantic_predictions'][0]

# Load video

cap = cv2.VideoCapture('surveillance_video.mp4')

while cap.isOpened():

ret, frame = cap.read()

if not ret:

break

# Preprocess frame

input_tensor = tf.convert_to_tensor(frame)

input_tensor = input_tensor[tf.newaxis, ...]

# Run model

seg_map = run_model(input_tensor)

seg_map = tf.image.resize(seg_map, frame.shape[:2], method='nearest').numpy()

# Define the color for people class (class 15 in COCO dataset)

seg_map = (seg_map == 15).astype(np.uint8)

# Overlay the segmentation map on the original frame

mask = np.stack((seg_map,) * 3, axis=-1) * 255


overlay = cv2.addWeighted(frame, 0.7, mask, 0.3, 0)

# Display the result

cv2.imshow('Person Segmentation', overlay)

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()

cv2.destroyAllWindows()

Task-7

Build a computer vision-based QR Code Scanner designed for seamless contactless transactions,
ensuring the security and efficiency of mobile payments

pip install opencv-python pyzbar

import cv2

from pyzbar import pyzbar

def decode_qr_code(frame):

# Find and decode QR codes

qr_codes = pyzbar.decode(frame)

for qr_code in qr_codes:

x, y, w, h = qr_code.rect

# Draw a bounding box around the QR code

cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

qr_data = qr_code.data.decode('utf-8')

qr_type = qr_code.type

# Display the decoded data


text = f"{qr_data} ({qr_type})"

cv2.putText(frame, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

# For security, you can process the QR data here

process_qr_data(qr_data)

return frame

def process_qr_data(qr_data):

# Add your code here to handle the decoded QR data securely

print(f"Processing QR data: {qr_data}")

# Capture video from the default camera

cap = cv2.VideoCapture(0)

while True:

ret, frame = cap.read()

if not ret:

break

# Decode QR codes in the frame

frame = decode_qr_code(frame)

# Display the frame

cv2.imshow('QR Code Scanner', frame)

# Press 'q' to exit the loop

if cv2.waitKey(1) & 0xFF == ord('q'):

break

cap.release()
cv2.destroyAllWindows()

You might also like