-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DAY-13_SUMMER_TRAINING_AI/ML(VIDEO CAPTURING) 😎
- Loading branch information
1 parent
3081917
commit 166ff51
Showing
8 changed files
with
293 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Background Subtraction in a Video | ||
|
||
import cv2 | ||
|
||
path = r'C:\Users\pc\Downloads\Day-13\vid_mpeg4.mp4' | ||
vid = cv2.VideoCapture(path) | ||
|
||
if not vid.isOpened(): | ||
print("Error: Couldn't open video file.") | ||
exit() | ||
|
||
back_subtractor = cv2.createBackgroundSubtractorMOG2(varThreshold=70, detectShadows=False) | ||
back_subtractor2 = cv2.createBackgroundSubtractorKNN(dist2Threshold=5000, detectShadows=False) | ||
|
||
while True: | ||
ret, frame = vid.read() | ||
|
||
if not ret: | ||
break # No more frames to read, exit the loop | ||
|
||
mask_image = back_subtractor.apply(frame) | ||
mask2_image = back_subtractor2.apply(frame) | ||
|
||
cv2.imshow('mask', mask_image) | ||
cv2.imshow('mask2', mask2_image) | ||
cv2.imshow('org', frame) | ||
|
||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
break | ||
|
||
vid.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Jul 24 10:24:44 2024 | ||
@author: student | ||
""" | ||
import cv2; | ||
import numpy as np; | ||
|
||
#Copy the Path of the Vedio. | ||
path = r"C:\Users\pc\Downloads\Day-13\vid_mpeg4.mp4"; | ||
|
||
#Create the Vedio Reader Object. | ||
vid=cv2.VideoCapture(path); | ||
|
||
print(vid); | ||
print(vid.isOpened()); | ||
|
||
while(vid.isOpened()): | ||
val,frame=vid.read(); | ||
#If the frame is found to be captured. | ||
if(val): | ||
|
||
gray_im=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY); | ||
ycrcb_im=cv2.cvtColor(frame,cv2.COLOR_BGR2YCrCb); | ||
hsv_im=cv2.cvtColor(frame,cv2.COLOR_BGR2HSV); | ||
|
||
cv2.imshow('Frame',gray_im); | ||
cv2.imshow('HSV',hsv_im); | ||
cv2.imshow('YCrCb',ycrcb_im); | ||
cv2.imshow('Original',frame); | ||
if(cv2.waitKey(1)==ord('q')): | ||
break; | ||
|
||
#Close the Values of the Objects. | ||
vid.release(); | ||
cv2.destroyAllWindows(); | ||
print(frame_counter()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# using opencv read the video file anf run it frame | ||
#by frame | ||
# in python environment | ||
import cv2 | ||
import numpy as np | ||
# copy the path of the video | ||
#path = 'C:\\Users\\pc\\vid_mpeg4.mp4' | ||
path = r"C:\Users\pc\Downloads\Day-13\VIRAT_S_050201_05_000890_000944.mp4"; | ||
# mouse click event | ||
def mouseRGB (event, x,y,flag,param): | ||
if(event==cv2.EVENT_FLAG_LBUTTON): | ||
colorB = frame[x,y,0] | ||
colorG = frame[x,y,1] | ||
colorR = frame[x,y,2] | ||
print('BGR values:',colorB,colorG,colorR) | ||
print('corr:',x,y) | ||
|
||
cv2.namedWindow('Frame') | ||
cv2.setMouseCallback('Frame', mouseRGB) | ||
# create the video reader object | ||
vid = cv2.VideoCapture(path) | ||
print(vid) | ||
print(vid.isOpened()) | ||
frame_counter=0 | ||
val,frame=vid.read() | ||
image_hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | ||
cv2.imshow('Frame',image_hsv) | ||
|
||
# ROI - region of interest Bmin - 71 , Bmax = 142 | ||
# Gmin - 94 , Gmax 218 | ||
# R min=120 R max = 235 | ||
while(1): | ||
if(cv2.waitKey(1)==ord('q')): | ||
break | ||
|
||
vid.release() # close the object | ||
cv2.destroyAllWindows() | ||
|
||
print(frame_counter) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
Created on Wed Jul 24 10:24:44 2024 | ||
@author: student | ||
""" | ||
|
||
#=========================================== Using the KNN! =============================================== | ||
|
||
import cv2; | ||
import numpy as np; | ||
import imutils; | ||
|
||
#Copy the Path of the Vedio. | ||
path = r"C:\Users\pc\Downloads\Day-13\VIRAT_S_050201_05_000890_000944.mp4"; | ||
|
||
#Backgroud Substractor. | ||
#bsck_gd = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(3,3)); | ||
back_substractor = cv2.createBackgroundSubtractorKNN(dist2Threshold=8000); | ||
|
||
#Create the Vedio Reader Object. | ||
vid=cv2.VideoCapture(path); | ||
print(vid); | ||
print(vid.isOpened()); | ||
frame_counter=0; | ||
#while(vid.isOpened()): | ||
f=0; | ||
while(f<=1000): | ||
f=f+1; | ||
val,frame=vid.read(); | ||
#Vedio Background Substractions. | ||
mask_image = back_substractor.apply(frame); | ||
cnts=cv2.findContours(mask_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE); | ||
final_contours=imutils.grab_contours(cnts); | ||
|
||
for c in final_contours: | ||
area=cv2.contourArea(c); | ||
if(area>250): | ||
print(area); | ||
M=cv2.moments(c); | ||
cx=int(M['m10']/M['m00']); | ||
cy=int(M['m01']/M['m00']); | ||
cv2.drawContours(frame,[c],-1,(0,0,255)); | ||
cv2.circle(frame,(cx,cy),4,(0,255,0)); | ||
|
||
cv2.imshow('mask',mask_image); | ||
cv2.imshow('original',frame); | ||
if(cv2.waitKey(1)==ord('q')): | ||
break; | ||
|
||
#Close the Values of the Objects. | ||
vid.release(); | ||
cv2.destroyAllWindows(); | ||
print(frame_counter()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import cv2 | ||
import numpy as np | ||
import imutils | ||
|
||
# Define HSV thresholds for the color of interest | ||
lowHSV = np.array([75, 100, 119]) | ||
highHSV = np.array([142, 218, 235]) | ||
|
||
# IP Webcam server address (replace with your device's IP and port) | ||
ip_address = '192.168.137.188'; # Example: '192.168.0.100' | ||
port = '4747'; # Example: '8080' | ||
url = f'http://{ip_address}:{port}/video'; # Correctly formatted URL string | ||
|
||
# Initialize video capture from IP Webcam stream | ||
vid = cv2.VideoCapture(url) | ||
|
||
frame_counter = 0 | ||
xCORR = [] | ||
yCORR = [] | ||
|
||
while vid.isOpened(): | ||
ret, frame = vid.read() | ||
frame_counter += 1 | ||
|
||
if ret: | ||
# Convert frame to HSV | ||
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | ||
|
||
# Create a mask using HSV thresholds | ||
mask = cv2.inRange(hsv, lowHSV, highHSV) | ||
|
||
# Find contours in the mask | ||
contours = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | ||
contours = imutils.grab_contours(contours) | ||
|
||
# Process each contour | ||
for c in contours: | ||
area = cv2.contourArea(c) | ||
if area > 200: | ||
M = cv2.moments(c) | ||
if M['m00'] != 0: | ||
cx = int(M['m10'] / M['m00']) | ||
cy = int(M['m01'] / M['m00']) | ||
cv2.drawContours(frame, [c], -1, (0, 0, 255), 2) | ||
cv2.circle(frame, (cx, cy), 4, (0, 255, 0), -1) | ||
xCORR.append(cx) | ||
yCORR.append(cy) | ||
frame[cy, cx] = (0, 0, 255) | ||
|
||
cv2.imshow('Frame', frame) | ||
|
||
if cv2.waitKey(1) & 0xFF == ord('q'): | ||
break | ||
|
||
vid.release() | ||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# color based ROI detection | ||
# ROI - region of interest Bmin - 71 , Bmax = 142 | ||
# Gmin - 94 , Gmax 218 | ||
# R min=120 R max = 235 | ||
|
||
import cv2 | ||
import numpy as np | ||
import imutils | ||
h=75;s=100;v=119 | ||
lowBGR = np.array([0,98,115]) | ||
highBGR =np.array([99,157,255]) | ||
# copy the path of the video | ||
#path = 'C:\\Users\\pc\\vid_mpeg4.mp4' | ||
path = r"C:\Users\pc\Downloads\Day-13\VIRAT_S_050201_05_000890_000944.mp4"; | ||
|
||
# create the video reader object | ||
vid = cv2.VideoCapture(path) | ||
print(vid) | ||
print(vid.isOpened()) | ||
frame_counter=0 | ||
xCORR= [] | ||
yCORR= [] | ||
c_list = [] | ||
while(vid.isOpened()): | ||
val,frame=vid.read() | ||
image_hsv=cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | ||
frame_counter +=1 | ||
# if the frame is captured | ||
if(val): | ||
mask_image = cv2.inRange(image_hsv, | ||
(lowBGR), | ||
(highBGR)) | ||
# find contours | ||
cnts = cv2.findContours(mask_image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | ||
final_contours = imutils.grab_contours(cnts) | ||
if(frame_counter==1): | ||
frame_original=frame.copy() | ||
|
||
count=0 | ||
#big object detection | ||
for c in final_contours: | ||
|
||
area = cv2.contourArea(c) | ||
if(area>200): | ||
print(area) | ||
count=count+1 | ||
print(count) | ||
|
||
M = cv2.moments(c) | ||
cx = int(M['m10']/M['m00']) | ||
cy = int(M['m01']/M['m00']) | ||
# c have every info about contour | ||
cv2.drawContours(frame, [c], -1, (0,0,255)) | ||
cv2.circle(frame, (cx,cy), 4 ,(0,255,0)) | ||
xCORR.append(cx) | ||
yCORR.append(cy) | ||
line_thickness = 2 | ||
|
||
for i in range(len(xCORR)): | ||
frame[yCORR[i],xCORR[i]]= (0,0,255) | ||
|
||
|
||
cv2.imshow('Frame',frame) | ||
#cv2.imshow('mask_image',mask_image) | ||
|
||
if(cv2.waitKey(1)==ord('q')): | ||
break | ||
|
||
|
||
|
||
vid.release() # close the object | ||
cv2.destroyAllWindows() | ||
|
||
print(frame_counter) |
Binary file not shown.