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 = [Link]("[Link]", "[Link]")
layer_names = [Link]()
output_layers = [layer_names[i - 1] for i in [Link]()]
# Load the COCO class labels YOLO model was trained on
classes = open("[Link]").read().strip().split("\n")
vehicle_classes = ["car", "bus", "truck", "motorbike", "bicycle"]
# Load video
cap = [Link]('traffic_video.mp4')
while [Link]():
ret, frame = [Link]()
if not ret:
break
height, width = [Link][:2]
blob = [Link](frame, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
[Link](blob)
outs = [Link](output_layers)
vehicle_count = 0
for out in outs:
for detection in out:
scores = detection[5:]
class_id = [Link](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
[Link](frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
[Link](frame, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
[Link](frame, f"Vehicle Count: {vehicle_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 0, 0), 2)
[Link]('Traffic Monitoring', frame)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
__________________________________________________________________________________
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("[Link]
# Function to run the model and get segmentation results
def run_model(image):
image = [Link](image, [513, 513])
image = [Link](image, tf.uint8)
result = model(image)
return result['semantic_predictions'][0]
# Load video
cap = [Link]('surveillance_video.mp4')
while [Link]():
ret, frame = [Link]()
if not ret:
break
# Preprocess frame
input_tensor = tf.convert_to_tensor(frame)
input_tensor = input_tensor[[Link], ...]
# Run model
seg_map = run_model(input_tensor)
seg_map = [Link](seg_map, [Link][: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 = [Link]((seg_map,) * 3, axis=-1) * 255
overlay = [Link](frame, 0.7, mask, 0.3, 0)
# Display the result
[Link]('Person Segmentation', overlay)
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()
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 = [Link](frame)
for qr_code in qr_codes:
x, y, w, h = qr_code.rect
# Draw a bounding box around the QR code
[Link](frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
qr_data = qr_code.[Link]('utf-8')
qr_type = qr_code.type
# Display the decoded data
text = f"{qr_data} ({qr_type})"
[Link](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 = [Link](0)
while True:
ret, frame = [Link]()
if not ret:
break
# Decode QR codes in the frame
frame = decode_qr_code(frame)
# Display the frame
[Link]('QR Code Scanner', frame)
# Press 'q' to exit the loop
if [Link](1) & 0xFF == ord('q'):
break
[Link]()
[Link]()