diff --git a/Assignment 2/01_drawing.py b/Assignment 2/01_drawing.py new file mode 100644 index 0000000..be04165 --- /dev/null +++ b/Assignment 2/01_drawing.py @@ -0,0 +1,32 @@ +import cv2 + +# read webcam +webcam = cv2.VideoCapture(0) + +# visualize webcam + +while True: + ret, frame = webcam.read() + + # Three vertices(tuples) of the triangle + p1 = (0, 20) + p2 = (640, 20) + p3 = (320, 480) + + # Drawing the triangle with the help of lines + # on the black window With given points + # cv2.line is the inbuilt function in opencv library + cv2.line(frame, p1, p2, (255, 0, 0), 3) + cv2.line(frame, p2, p3, (255, 0, 0), 3) + cv2.line(frame, p1, p3, (255, 0, 0), 3) + + cv2.putText(frame, 'Hello World', (240, 240), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 4) + + cv2.imshow('frame', frame) + if cv2.waitKey(40) & 0xFF == ord('q'): + break + + + +webcam.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Assignment 2/02_coin_detector.py b/Assignment 2/02_coin_detector.py new file mode 100644 index 0000000..108727a --- /dev/null +++ b/Assignment 2/02_coin_detector.py @@ -0,0 +1,42 @@ +import cv2 +import numpy as np + +# read webcam +webcam = cv2.VideoCapture(0) + +# visualize webcam + +while True: + ret, frame = webcam.read() + + Blur = cv2.medianBlur(frame,3) + dimensions = frame.shape + + grayimg = cv2.cvtColor(Blur, cv2.COLOR_BGR2GRAY) + circles = cv2.HoughCircles(grayimg, cv2.HOUGH_GRADIENT,1.2,dimensions[0]/50) + + thickness = 2 + #Draw detected circles + if circles is not None: + print("Found circle") + circles = np.uint16(circles[0,:]) + #print(circles) + print(type(circles.shape)) + for (x, y, diameter) in circles: + #draw the outer circle + cv2.circle(frame, (x,y), diameter, (0,0,255), thickness, cv2.LINE_AA) + #draw the outer circle + cv2.circle(frame, (x,y), 2, (0,0,255), thickness, cv2.LINE_AA) + cv2.putText(frame, 'Total Circles: ' + str(circles.shape[0]), (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 4) + + else: + print("Cannot detect circle.") + + cv2.imshow('frame', frame) + if cv2.waitKey(40) & 0xFF == ord('q'): + break + + + +webcam.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Assignment 2/03_lane_detection.py b/Assignment 2/03_lane_detection.py new file mode 100644 index 0000000..cac5f7b --- /dev/null +++ b/Assignment 2/03_lane_detection.py @@ -0,0 +1,194 @@ +import numpy as np +import pandas as pd +import cv2 +#from google.colab.patches import cv2_imshow +#Import everything needed to edit/save/watch video clips +from moviepy import editor +import moviepy + +def region_selection(image): + """ + Determine and cut the region of interest in the input image. + Parameters: + image: we pass here the output from canny where we have + identified edges in the frame + """ + # create an array of the same size as of the input image + mask = np.zeros_like(image) + # if you pass an image with more then one channel + if len(image.shape) > 2: + channel_count = image.shape[2] + ignore_mask_color = (255,) * channel_count + # our image only has one channel so it will go under "else" + else: + # color of the mask polygon (white) + ignore_mask_color = 255 + # creating a polygon to focus only on the road in the picture + # we have created this polygon in accordance to how the camera was placed + rows, cols = image.shape[:2] + bottom_left = [cols * 0.1, rows * 0.95] + top_left = [cols * 0.4, rows * 0.6] + bottom_right = [cols * 0.9, rows * 0.95] + top_right = [cols * 0.6, rows * 0.6] + vertices = np.array([[bottom_left, top_left, top_right, bottom_right]], dtype=np.int32) + # filling the polygon with white color and generating the final mask + cv2.fillPoly(mask, vertices, ignore_mask_color) + # performing Bitwise AND on the input image and mask to get only the edges on the road + masked_image = cv2.bitwise_and(image, mask) + return masked_image + +def hough_transform(image): + """ + Determine and cut the region of interest in the input image. + Parameter: + image: grayscale image which should be an output from the edge detector + """ + # Distance resolution of the accumulator in pixels. + rho = 1 + # Angle resolution of the accumulator in radians. + theta = np.pi/180 + # Only lines that are greater than threshold will be returned. + threshold = 20 + # Line segments shorter than that are rejected. + minLineLength = 20 + # Maximum allowed gap between points on the same line to link them + maxLineGap = 500 + # function returns an array containing dimensions of straight lines + # appearing in the input image + return cv2.HoughLinesP(image, rho = rho, theta = theta, threshold = threshold, + minLineLength = minLineLength, maxLineGap = maxLineGap) + +def average_slope_intercept(lines): + """ + Find the slope and intercept of the left and right lanes of each image. + Parameters: + lines: output from Hough Transform + """ + left_lines = [] #(slope, intercept) + left_weights = [] #(length,) + right_lines = [] #(slope, intercept) + right_weights = [] #(length,) + + for line in lines: + for x1, y1, x2, y2 in line: + if x1 == x2: + continue + # calculating slope of a line + slope = (y2 - y1) / (x2 - x1) + # calculating intercept of a line + intercept = y1 - (slope * x1) + # calculating length of a line + length = np.sqrt(((y2 - y1) ** 2) + ((x2 - x1) ** 2)) + # slope of left lane is negative and for right lane slope is positive + if slope < 0: + left_lines.append((slope, intercept)) + left_weights.append((length)) + else: + right_lines.append((slope, intercept)) + right_weights.append((length)) + # + left_lane = np.dot(left_weights, left_lines) / np.sum(left_weights) if len(left_weights) > 0 else None + right_lane = np.dot(right_weights, right_lines) / np.sum(right_weights) if len(right_weights) > 0 else None + return left_lane, right_lane + +def pixel_points(y1, y2, line): + """ + Converts the slope and intercept of each line into pixel points. + Parameters: + y1: y-value of the line's starting point. + y2: y-value of the line's end point. + line: The slope and intercept of the line. + """ + if line is None: + return None + slope, intercept = line + x1 = int((y1 - intercept)/slope) + x2 = int((y2 - intercept)/slope) + y1 = int(y1) + y2 = int(y2) + return ((x1, y1), (x2, y2)) + +def lane_lines(image, lines): + """ + Create full lenght lines from pixel points. + Parameters: + image: The input test image. + lines: The output lines from Hough Transform. + """ + left_lane, right_lane = average_slope_intercept(lines) + y1 = image.shape[0] + y2 = y1 * 0.6 + left_line = pixel_points(y1, y2, left_lane) + right_line = pixel_points(y1, y2, right_lane) + return left_line, right_line + + +def draw_lane_lines(image, lines, color=[255, 0, 0], thickness=12): + """ + Draw lines onto the input image. + Parameters: + image: The input test image (video frame in our case). + lines: The output lines from Hough Transform. + color (Default = red): Line color. + thickness (Default = 12): Line thickness. + """ + line_image = np.zeros_like(image) + for line in lines: + if line is not None: + cv2.line(line_image, *line, color, thickness) + return cv2.addWeighted(image, 1.0, line_image, 1.0, 0.0) + +def frame_processor(image): + """ + Process the input frame to detect lane lines. + Parameters: + image: image of a road where one wants to detect lane lines + (we will be passing frames of video to this function) + """ + # convert the RGB image to Gray scale + grayscale = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + # applying gaussian Blur which removes noise from the image + # and focuses on our region of interest + # size of gaussian kernel + kernel_size = 5 + # Applying gaussian blur to remove noise from the frames + blur = cv2.GaussianBlur(grayscale, (kernel_size, kernel_size), 0) + # first threshold for the hysteresis procedure + low_t = 50 + # second threshold for the hysteresis procedure + high_t = 150 + # applying canny edge detection and save edges in a variable + edges = cv2.Canny(blur, low_t, high_t) + # since we are getting too many edges from our image, we apply + # a mask polygon to only focus on the road + # Will explain Region selection in detail in further steps + region = region_selection(edges) + # Applying hough transform to get straight lines from our image + # and find the lane lines + # Will explain Hough Transform in detail in further steps + hough = hough_transform(region) + #lastly we draw the lines on our resulting frame and return it as output + result = draw_lane_lines(image, lane_lines(image, hough)) + return result + +# driver function +def process_video(test_video, output_video): + """ + Read input video stream and produce a video file with detected lane lines. + Parameters: + test_video: location of input video file + output_video: location where output video file is to be saved + """ + # read the video file using VideoFileClip without audio + input_video = editor.VideoFileClip(test_video, audio=False) + # apply the function "frame_processor" to each frame of the video + # will give more detail about "frame_processor" in further steps + # "processed" stores the output video + processed = input_video.fl_image(frame_processor) + # save the output video stream to an mp4 file + processed.write_videofile(output_video, audio=False) + +# calling driver function +#process_video('input.mp4','output.mp4') + + diff --git a/Assignment 2/03_lane_detection_show.py b/Assignment 2/03_lane_detection_show.py new file mode 100644 index 0000000..30169b2 --- /dev/null +++ b/Assignment 2/03_lane_detection_show.py @@ -0,0 +1,35 @@ +import numpy as np +import cv2 + +names = ['input.mp4', 'output.mp4'] +window_titles = ['input', 'output'] + + +cap = [cv2.VideoCapture(i) for i in names] + +frames = [None] * len(names); +gray = [None] * len(names); +ret = [None] * len(names); + +while True: + + for i,c in enumerate(cap): + if c is not None: + ret[i], frames[i] = c.read(); + + + for i,f in enumerate(frames): + if ret[i] is True: + #gray[i] = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY) + #cv2.imshow(window_titles[i], gray[i]); + cv2.imshow(window_titles[i], frames[i]); + + if cv2.waitKey(1) & 0xFF == ord('q'): + break + + +for c in cap: + if c is not None: + c.release(); + +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Assignment 2/04_FrontalFaceDetection.py b/Assignment 2/04_FrontalFaceDetection.py new file mode 100644 index 0000000..9a66479 --- /dev/null +++ b/Assignment 2/04_FrontalFaceDetection.py @@ -0,0 +1,34 @@ +import face_recognition +import cv2 + +# read webcam +video_capture = cv2.VideoCapture(0) + +# visualize webcam +winname = "Test" + +while True: + ret, frame = video_capture.read() + + face_location = face_recognition.face_locations(frame) + print(face_location) + #print("There are " + str(len(face_location)) + " people in this image.") + + if(len(face_location) > 0): + for index in range(0, len(face_location)): + x0 = face_location[index][3] #left + y0 = face_location[index][0] #Top + x1 = face_location[index][1] #Bottom + y1 = face_location[index][2] #Right + cv2.rectangle(frame, (x0, y0), (x1,y1), (255,0,0),3) + + cv2.putText(frame, 'Total People: ' + str(len(face_location)), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 0), 4) + + cv2.namedWindow(winname) + cv2.moveWindow(winname, 40,30) # Move it to (40,30) + cv2.imshow('frame', frame) + if cv2.waitKey(1) & 0xFF == ord('q'): + break + +webcam.release() +cv2.destroyAllWindows() \ No newline at end of file diff --git a/Assignment 2/check_resolution.py b/Assignment 2/check_resolution.py new file mode 100644 index 0000000..20400c2 --- /dev/null +++ b/Assignment 2/check_resolution.py @@ -0,0 +1,31 @@ +import cv2 + +vcap = cv2.VideoCapture(0) # built-in webcamera + +#vcap = cv2.VideoCapture('video.avi') + +if vcap.isOpened(): + width = vcap.get(cv2.CAP_PROP_FRAME_WIDTH) # float `width` + height = vcap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float `height` + # or + width = vcap.get(3) # float `width` + height = vcap.get(4) # float `height` + + print('width, height:', width, height) + + fps = vcap.get(cv2.CAP_PROP_FPS) + # or + fps = vcap.get(5) + + print('fps:', fps) # float `fps` + + frame_count = vcap.get(cv2.CAP_PROP_FRAME_COUNT) + # or + frame_count = vcap.get(7) + + print('frames count:', frame_count) # float `frame_count` + + #print('cv2.CAP_PROP_FRAME_WIDTH :', cv2.CAP_PROP_FRAME_WIDTH) # 3 + #print('cv2.CAP_PROP_FRAME_HEIGHT:', cv2.CAP_PROP_FRAME_HEIGHT) # 4 + #print('cv2.CAP_PROP_FPS :', cv2.CAP_PROP_FPS) # 5 + #print('cv2.CAP_PROP_FRAME_COUNT :', cv2.CAP_PROP_FRAME_COUNT) # 7 \ No newline at end of file diff --git a/Assignment 2/input.mp4 b/Assignment 2/input.mp4 new file mode 100644 index 0000000..13e6d1c Binary files /dev/null and b/Assignment 2/input.mp4 differ diff --git a/Assignment 2/output.mp4 b/Assignment 2/output.mp4 new file mode 100644 index 0000000..aa4d4df Binary files /dev/null and b/Assignment 2/output.mp4 differ