├── LICENSE ├── Computer Vision Fundamentals ├── 01 Core_Image_Processing │ ├── 01 Image_Basics │ │ └── image_basics.py │ ├── 04 Color_Processing │ │ └── color_processing.py │ ├── 03 Image_Filtering │ │ └── image_filtering.py │ ├── 02 Image_Transformations │ │ └── image_transformations.py │ └── README.md ├── 03 Video_Processing │ ├── 01 Video_Basics │ │ └── video_basics.py │ ├── 02 Video_Analysis │ │ └── video_analysis.py │ ├── 03 Object_Tracking │ │ └── object_tracking.py │ └── README.md ├── 04 Object_Detection_and_Recognition │ ├── 01 Template_Matching │ │ └── template_matching.py │ ├── 02 Haar_Cascades │ │ └── haar_cascades.py │ ├── 04 Image_Classification │ │ └── image_classification.py │ ├── 03 Deep_Learning_Based_Detection │ │ └── deep_learning_detection.py │ └── README.md ├── 02 Advanced_Image_Processing │ ├── 03 Optical_Flow │ │ └── optical_flow.py │ ├── 02 Image_Segmentation │ │ └── image_segmentation.py │ ├── 01 Feature_Detection │ │ └── feature_detection.py │ └── README.md └── 05 Optimization_and_Deployment │ ├── 01 Performance_Optimization │ └── performance_optimization.py │ ├── 03 Integration │ └── integration.py │ ├── 02 Deployment │ └── deployment.py │ └── README.md ├── README.md └── Computer Vision Interview Questions └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2025 rohanmistry231 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/01 Image_Basics/image_basics.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Basics] 2 | # Learn to read/write images, manipulate pixels, and explore color spaces with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_basics_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a red rectangle 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [0, 0, 255] # Red square (BGR format) 14 | print("Synthetic Image: 300x300 RGB with red square created") 15 | 16 | # %% [3. Reading/Writing Images] 17 | cv2.imwrite("synthetic_image.png", img) 18 | img_read = cv2.imread("synthetic_image.png") 19 | print("Image Read/Written: synthetic_image.png") 20 | 21 | # %% [4. Color Spaces] 22 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 23 | img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 24 | print(f"Color Spaces: Converted to Grayscale and HSV") 25 | 26 | # %% [5. Pixel Manipulation] 27 | img[50:100, 50:100] = [255, 0, 0] # Add blue square 28 | print("Pixel Manipulation: Added blue square at (50:100, 50:100)") 29 | 30 | # %% [6. Image Properties] 31 | height, width, channels = img.shape 32 | print(f"Image Properties: Height={height}, Width={width}, Channels={channels}") 33 | 34 | # %% [7. Visualization] 35 | plt.figure(figsize=(10, 8)) 36 | plt.subplot(1, 3, 1) 37 | plt.title("Original (BGR)") 38 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 39 | plt.subplot(1, 3, 2) 40 | plt.title("Grayscale") 41 | plt.imshow(img_gray, cmap="gray") 42 | plt.subplot(1, 3, 3) 43 | plt.title("HSV") 44 | plt.imshow(cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)) 45 | plt.savefig("image_basics_output.png") 46 | print("Visualization: Images saved as image_basics_output.png") 47 | 48 | # %% [8. Interview Scenario: Image Basics] 49 | """ 50 | Interview Scenario: Image Basics 51 | Q: What’s the difference between RGB and HSV color spaces? 52 | A: RGB defines colors by red, green, blue intensities; HSV uses hue, saturation, value for intuitive color manipulation. 53 | Key: HSV is better for color-based segmentation (e.g., filtering specific hues). 54 | Example: cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 55 | """ 56 | 57 | # Execute the demo 58 | if __name__ == "__main__": 59 | run_image_basics_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/01 Video_Basics/video_basics.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Video Basics] 2 | # Learn to read/write videos, extract frames, and access video properties with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_video_basics_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate a sequence of 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | print("Synthetic Video: 10 frames with moving white square created") 19 | 20 | # %% [3. Write Video] 21 | height, width = 300, 300 22 | fps = 10 23 | out = cv2.VideoWriter("synthetic_video.mp4", cv2.VideoWriter_fourcc(*"mp4v"), fps, (width, height)) 24 | for frame in frames: 25 | out.write(frame) 26 | out.release() 27 | print("Video Written: synthetic_video.mp4 saved") 28 | 29 | # %% [4. Read Video and Extract Frames] 30 | cap = cv2.VideoCapture("synthetic_video.mp4") 31 | extracted_frames = [] 32 | while cap.isOpened(): 33 | ret, frame = cap.read() 34 | if not ret: 35 | break 36 | extracted_frames.append(frame) 37 | cap.release() 38 | print(f"Video Read: {len(extracted_frames)} frames extracted") 39 | 40 | # %% [5. Video Properties] 41 | cap = cv2.VideoCapture("synthetic_video.mp4") 42 | fps = cap.get(cv2.CAP_PROP_FPS) 43 | width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 44 | height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 45 | frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) 46 | print(f"Video Properties: FPS={fps}, Resolution={width}x{height}, Frames={frame_count}") 47 | cap.release() 48 | 49 | # %% [6. Visualization] 50 | plt.figure(figsize=(15, 5)) 51 | for i, idx in enumerate([0, 4, 9]): # Show first, middle, last frames 52 | plt.subplot(1, 3, i+1) 53 | plt.title(f"Frame {idx+1}") 54 | plt.imshow(cv2.cvtColor(extracted_frames[idx], cv2.COLOR_BGR2RGB)) 55 | plt.savefig("video_basics_output.png") 56 | print("Visualization: Frames saved as video_basics_output.png") 57 | 58 | # %% [7. Interview Scenario: Video Basics] 59 | """ 60 | Interview Scenario: Video Basics 61 | Q: How do you read a video and extract its properties in OpenCV? 62 | A: Use cv2.VideoCapture to read frames and get properties like FPS, resolution. 63 | Key: Properties are accessed via cap.get(cv2.CAP_PROP_*). 64 | Example: cap.get(cv2.CAP_PROP_FPS) 65 | """ 66 | 67 | # Execute the demo 68 | if __name__ == "__main__": 69 | run_video_basics_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/01 Template_Matching/template_matching.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Template Matching] 2 | # Learn single and multi-object template matching with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_template_matching_demo(): 10 | # %% [2. Create Synthetic Image and Template] 11 | # Generate a 300x300 image with multiple white squares 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[50:100, 50:100] = [255, 255, 255] # Square 1 14 | img[150:200, 150:200] = [255, 255, 255] # Square 2 15 | img[250:300, 50:100] = [255, 255, 255] # Square 3 16 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | 18 | # Create a 50x50 template (white square) 19 | template = np.ones((50, 50), dtype=np.uint8) * 255 20 | print("Synthetic Image: 300x300 with three white squares; Template: 50x50 white square") 21 | 22 | # %% [3. Single Object Matching] 23 | result = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) 24 | _, max_val, _, max_loc = cv2.minMaxLoc(result) 25 | img_single = img.copy() 26 | top_left = max_loc 27 | bottom_right = (top_left[0] + 50, top_left[1] + 50) 28 | cv2.rectangle(img_single, top_left, bottom_right, (0, 0, 255), 2) 29 | print(f"Single Object Matching: Best match at {max_loc}, score={max_val:.2f}") 30 | 31 | # %% [4. Multi-Object Matching] 32 | threshold = 0.8 33 | loc = np.where(result >= threshold) 34 | img_multi = img.copy() 35 | for pt in zip(*loc[::-1]): 36 | bottom_right = (pt[0] + 50, pt[1] + 50) 37 | cv2.rectangle(img_multi, pt, bottom_right, (0, 255, 0), 2) 38 | print(f"Multi-Object Matching: {len(loc[0])} matches found with threshold={threshold}") 39 | 40 | # %% [5. Visualization] 41 | plt.figure(figsize=(15, 5)) 42 | plt.subplot(1, 3, 1) 43 | plt.title("Original Image") 44 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 45 | plt.subplot(1, 3, 2) 46 | plt.title("Single Match") 47 | plt.imshow(cv2.cvtColor(img_single, cv2.COLOR_BGR2RGB)) 48 | plt.subplot(1, 3, 3) 49 | plt.title("Multi Match") 50 | plt.imshow(cv2.cvtColor(img_multi, cv2.COLOR_BGR2RGB)) 51 | plt.savefig("template_matching_output.png") 52 | print("Visualization: Template matching results saved as template_matching_output.png") 53 | 54 | # %% [6. Interview Scenario: Template Matching] 55 | """ 56 | Interview Scenario: Template Matching 57 | Q: How does template matching work, and what are its limitations? 58 | A: Template matching slides a template over an image, computing similarity (e.g., normalized correlation). 59 | Key: Limited to fixed-scale, rotation-invariant templates; sensitive to lighting changes. 60 | Example: cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED) 61 | """ 62 | 63 | # Execute the demo 64 | if __name__ == "__main__": 65 | run_template_matching_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/02 Haar_Cascades/haar_cascades.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Haar Cascades] 2 | # Learn face and eye detection using pretrained Haar cascades with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_haar_cascades_demo(): 10 | # %% [2. Create Synthetic Face Image] 11 | # Generate a 300x300 image with a simplified face (ovals for face and eyes) 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | cv2.ellipse(img, (150, 150), (100, 120), 0, 0, 360, (200, 200, 200), -1) # Face 14 | cv2.ellipse(img, (120, 120), (20, 30), 0, 0, 360, (50, 50, 50), -1) # Left eye 15 | cv2.ellipse(img, (180, 120), (20, 30), 0, 0, 360, (50, 50, 50), -1) # Right eye 16 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | print("Synthetic Image: 300x300 with simplified face and eyes created") 18 | 19 | # %% [3. Load Haar Cascade Classifiers] 20 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") 21 | eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_eye.xml") 22 | print("Haar Cascades: Loaded face and eye classifiers") 23 | 24 | # %% [4. Face Detection] 25 | faces = face_cascade.detectMultiScale(img_gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)) 26 | img_faces = img.copy() 27 | for (x, y, w, h) in faces: 28 | cv2.rectangle(img_faces, (x, y), (x+w, y+h), (0, 0, 255), 2) 29 | print(f"Face Detection: {len(faces)} faces detected") 30 | 31 | # %% [5. Eye Detection] 32 | eyes = eye_cascade.detectMultiScale(img_gray, scaleFactor=1.1, minNeighbors=5, minSize=(20, 20)) 33 | img_eyes = img.copy() 34 | for (x, y, w, h) in eyes: 35 | cv2.rectangle(img_eyes, (x, y), (x+w, y+h), (0, 255, 0), 2) 36 | print(f"Eye Detection: {len(eyes)} eyes detected") 37 | 38 | # %% [6. Visualization] 39 | plt.figure(figsize=(15, 5)) 40 | plt.subplot(1, 3, 1) 41 | plt.title("Original Image") 42 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 43 | plt.subplot(1, 3, 2) 44 | plt.title("Face Detection") 45 | plt.imshow(cv2.cvtColor(img_faces, cv2.COLOR_BGR2RGB)) 46 | plt.subplot(1, 3, 3) 47 | plt.title("Eye Detection") 48 | plt.imshow(cv2.cvtColor(img_eyes, cv2.COLOR_BGR2RGB)) 49 | plt.savefig("haar_cascades_output.png") 50 | print("Visualization: Haar cascade results saved as haar_cascades_output.png") 51 | 52 | # %% [7. Interview Scenario: Haar Cascades] 53 | """ 54 | Interview Scenario: Haar Cascades 55 | Q: What are the limitations of Haar cascades for face detection? 56 | A: Haar cascades are fast but struggle with non-frontal faces, occlusions, and varying lighting. 57 | Key: Use integral images for efficiency but less robust than deep learning. 58 | Example: cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml") 59 | """ 60 | 61 | # Execute the demo 62 | if __name__ == "__main__": 63 | run_haar_cascades_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/04 Color_Processing/color_processing.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Color Processing] 2 | # Learn color space conversion, color filtering, histogram equalization, and quantization with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_color_processing_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a blue-to-red gradient 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | for i in range(300): 14 | img[i, :, 0] = i % 255 # Blue gradient 15 | img[i, :, 2] = 255 - (i % 255) # Red gradient 16 | print("Synthetic Image: 300x300 RGB with blue-to-red gradient created") 17 | 18 | # %% [3. Color Space Conversion] 19 | img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 20 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 21 | print("Color Space Conversion: Converted to HSV and Grayscale") 22 | 23 | # %% [4. Color Filtering] 24 | # Filter blue colors in HSV 25 | lower_blue = np.array([100, 50, 50]) 26 | upper_blue = np.array([130, 255, 255]) 27 | mask = cv2.inRange(img_hsv, lower_blue, upper_blue) 28 | img_filtered = cv2.bitwise_and(img, img, mask=mask) 29 | print("Color Filtering: Isolated blue colors") 30 | 31 | # %% [5. Histogram Equalization] 32 | img_eq = cv2.equalizeHist(img_gray) 33 | print("Histogram Equalization: Applied to Grayscale image") 34 | 35 | # %% [6. Color Quantization] 36 | # Reduce colors using K-Means 37 | Z = img.reshape((-1, 3)).astype(np.float32) 38 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 39 | K = 4 40 | _, labels, centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) 41 | centers = np.uint8(centers) 42 | img_quantized = centers[labels.flatten()].reshape(img.shape) 43 | print(f"Color Quantization: Reduced to {K} colors") 44 | 45 | # %% [7. Visualization] 46 | plt.figure(figsize=(15, 8)) 47 | plt.subplot(2, 3, 1) 48 | plt.title("Original") 49 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 50 | plt.subplot(2, 3, 2) 51 | plt.title("HSV") 52 | plt.imshow(cv2.cvtColor(img_hsv, cv2.COLOR_HSV2RGB)) 53 | plt.subplot(2, 3, 3) 54 | plt.title("Grayscale") 55 | plt.imshow(img_gray, cmap="gray") 56 | plt.subplot(2, 3, 4) 57 | plt.title("Blue Filtered") 58 | plt.imshow(cv2.cvtColor(img_filtered, cv2.COLOR_BGR2RGB)) 59 | plt.subplot(2, 3, 5) 60 | plt.title("Equalized") 61 | plt.imshow(img_eq, cmap="gray") 62 | plt.subplot(2, 3, 6) 63 | plt.title("Quantized") 64 | plt.imshow(cv2.cvtColor(img_quantized, cv2.COLOR_BGR2RGB)) 65 | plt.savefig("color_processing_output.png") 66 | print("Visualization: Color processing saved as color_processing_output.png") 67 | 68 | # %% [8. Interview Scenario: Color Processing] 69 | """ 70 | Interview Scenario: Color Processing 71 | Q: Why is HSV useful for color filtering compared to RGB? 72 | A: HSV separates hue (color type) from saturation and value, making it easier to isolate specific colors. 73 | Key: Use cv2.inRange in HSV for robust color segmentation. 74 | Example: cv2.inRange(img_hsv, lower_blue, upper_blue) 75 | """ 76 | 77 | # Execute the demo 78 | if __name__ == "__main__": 79 | run_color_processing_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/04 Image_Classification/image_classification.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Classification] 2 | # Learn pretrained MobileNetV2 classification and transfer learning with TensorFlow. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import tensorflow as tf 9 | from tensorflow.keras.applications import MobileNetV2 10 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 11 | 12 | def run_image_classification_demo(): 13 | # %% [2. Load CIFAR-10 Dataset] 14 | (x_train, y_train), _ = tf.keras.datasets.cifar10.load_data() 15 | class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'] 16 | img = x_train[0] # Example: airplane image 17 | print("CIFAR-10: Loaded sample image for classification") 18 | 19 | # %% [3. Pretrained MobileNetV2 Classification] 20 | model = MobileNetV2(weights='imagenet') 21 | img_resized = cv2.resize(img, (224, 224)) 22 | img_array = np.expand_dims(img_resized, axis=0) 23 | img_preprocessed = preprocess_input(img_array) 24 | predictions = model.predict(img_preprocessed) 25 | decoded_preds = decode_predictions(predictions, top=3)[0] 26 | img_pred = img.copy() 27 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 28 | cv2.putText(img_pred, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 29 | print(f"MobileNetV2: Top prediction: {decoded_preds[0][1]} ({decoded_preds[0][2]:.2f})") 30 | 31 | # %% [4. Transfer Learning (Simulated)] 32 | # Simulate fine-tuning MobileNetV2 for CIFAR-10 (subset) 33 | base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) 34 | base_model.trainable = False 35 | model = tf.keras.Sequential([ 36 | base_model, 37 | tf.keras.layers.GlobalAveragePooling2D(), 38 | tf.keras.layers.Dense(10, activation='softmax') 39 | ]) 40 | model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) 41 | model.fit(x_train[:1000], y_train[:1000], epochs=1, batch_size=32, verbose=0) 42 | pred = model.predict(x_train[:1]) 43 | class_id = np.argmax(pred[0]) 44 | print(f"Transfer Learning: Predicted class for sample: {class_names[class_id]}") 45 | 46 | # %% [5. Visualization] 47 | plt.figure(figsize=(10, 5)) 48 | plt.subplot(1, 2, 1) 49 | plt.title("Original Image") 50 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 51 | plt.subplot(1, 2, 2) 52 | plt.title("MobileNetV2 Prediction") 53 | plt.imshow(cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)) 54 | plt.savefig("image_classification_output.png") 55 | print("Visualization: Classification results saved as image_classification_output.png") 56 | 57 | # %% [6. Interview Scenario: Image Classification] 58 | """ 59 | Interview Scenario: Image Classification 60 | Q: How does transfer learning improve image classification? 61 | A: Transfer learning uses pretrained models (e.g., MobileNetV2) to leverage learned features, reducing training time and data needs. 62 | Key: Fine-tune top layers for specific tasks like CIFAR-10 classification. 63 | Example: MobileNetV2(weights='imagenet', include_top=False) 64 | """ 65 | 66 | # Execute the demo 67 | if __name__ == "__main__": 68 | run_image_classification_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/03 Optical_Flow/optical_flow.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Optical Flow] 2 | # Learn dense and sparse optical flow and motion tracking with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_optical_flow_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate two 300x300 frames with a moving white square 12 | frame1 = np.zeros((300, 300, 3), dtype=np.uint8) 13 | frame2 = np.zeros((300, 300, 3), dtype=np.uint8) 14 | frame1[100:150, 100:150] = [255, 255, 255] # White square 15 | frame2[110:160, 110:160] = [255, 255, 255] # Shifted 10px right, down 16 | frame1_gray = cv2.cvtColor(frame1, cv2.COLOR_BGR2GRAY) 17 | frame2_gray = cv2.cvtColor(frame2, cv2.COLOR_BGR2GRAY) 18 | print("Synthetic Frames: Two frames with moving white square created") 19 | 20 | # %% [3. Dense Optical Flow (Farneback)] 21 | flow = cv2.calcOpticalFlowFarneback(frame1_gray, frame2_gray, None, 0.5, 3, 15, 3, 5, 1.2, 0) 22 | mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1]) 23 | hsv = np.zeros_like(frame1) 24 | hsv[..., 1] = 255 25 | hsv[..., 0] = ang * 180 / np.pi / 2 26 | hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX) 27 | img_flow = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR) 28 | print("Dense Optical Flow: Farneback flow computed") 29 | 30 | # %% [4. Sparse Optical Flow (Lucas-Kanade)] 31 | corners = cv2.goodFeaturesToTrack(frame1_gray, maxCorners=10, qualityLevel=0.3, minDistance=7) 32 | p1, st, err = cv2.calcOpticalFlowPyrLK(frame1_gray, frame2_gray, corners, None) 33 | img_lk = frame2.copy() 34 | for i, (new, old) in enumerate(zip(p1[st==1], corners[st==1])): 35 | a, b = new.ravel() 36 | c, d = old.ravel() 37 | cv2.line(img_lk, (int(a), int(b)), (int(c), int(d)), (0, 255, 0), 2) 38 | cv2.circle(img_lk, (int(a), int(b)), 5, (0, 0, 255), -1) 39 | print("Sparse Optical Flow: Lucas-Kanade tracked keypoints") 40 | 41 | # %% [5. Motion Tracking] 42 | # Simulate tracking by drawing bounding box around moving square 43 | x, y = int(p1[st==1][0][0]), int(p1[st==1][0][1]) 44 | img_track = frame2.copy() 45 | cv2.rectangle(img_track, (x-25, y-25), (x+25, y+25), (0, 0, 255), 2) 46 | print("Motion Tracking: Bounding box drawn around moving square") 47 | 48 | # %% [6. Visualization] 49 | plt.figure(figsize=(15, 8)) 50 | plt.subplot(2, 2, 1) 51 | plt.title("Frame 1") 52 | plt.imshow(cv2.cvtColor(frame1, cv2.COLOR_BGR2RGB)) 53 | plt.subplot(2, 2, 2) 54 | plt.title("Frame 2") 55 | plt.imshow(cv2.cvtColor(frame2, cv2.COLOR_BGR2RGB)) 56 | plt.subplot(2, 2, 3) 57 | plt.title("Dense Flow") 58 | plt.imshow(cv2.cvtColor(img_flow, cv2.COLOR_BGR2RGB)) 59 | plt.subplot(2, 2, 4) 60 | plt.title("Lucas-Kanade") 61 | plt.imshow(cv2.cvtColor(img_lk, cv2.COLOR_BGR2RGB)) 62 | plt.savefig("optical_flow_output.png") 63 | print("Visualization: Optical flow results saved as optical_flow_output.png") 64 | 65 | # %% [7. Interview Scenario: Optical Flow] 66 | """ 67 | Interview Scenario: Optical Flow 68 | Q: What are the applications of optical flow in computer vision? 69 | A: Optical flow estimates motion for tracking, video stabilization, and activity recognition. 70 | Key: Dense flow (Farneback) is global; sparse flow (Lucas-Kanade) tracks specific points. 71 | Example: cv2.calcOpticalFlowFarneback(frame1, frame2, ...) 72 | """ 73 | 74 | # Execute the demo 75 | if __name__ == "__main__": 76 | run_optical_flow_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/03 Image_Filtering/image_filtering.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Filtering] 2 | # Learn blurring, sharpening, edge detection, thresholding, and morphological operations with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_filtering_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a white rectangle on black 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [255, 255, 255] # White square (BGR format) 14 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 15 | print("Synthetic Image: 300x300 RGB with white square created") 16 | 17 | # %% [3. Blurring] 18 | img_gaussian = cv2.GaussianBlur(img_gray, (5, 5), 0) 19 | img_median = cv2.medianBlur(img_gray, 5) 20 | img_bilateral = cv2.bilateralFilter(img_gray, 9, 75, 75) 21 | print("Blurring: Applied Gaussian, Median, and Bilateral filters") 22 | 23 | # %% [4. Sharpening] 24 | kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]], dtype=np.float32) 25 | img_sharpened = cv2.filter2D(img_gray, -1, kernel) 26 | print("Sharpening: Applied sharpening filter") 27 | 28 | # %% [5. Edge Detection] 29 | img_sobel = cv2.Sobel(img_gray, cv2.CV_64F, 1, 0, ksize=3) 30 | img_sobel = cv2.convertScaleAbs(img_sobel) 31 | img_canny = cv2.Canny(img_gray, 100, 200) 32 | print("Edge Detection: Applied Sobel and Canny") 33 | 34 | # %% [6. Thresholding] 35 | _, img_binary = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY) 36 | img_adaptive = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2) 37 | print("Thresholding: Applied Binary and Adaptive") 38 | 39 | # %% [7. Morphological Operations] 40 | kernel = np.ones((5,5), np.uint8) 41 | img_dilation = cv2.dilate(img_binary, kernel, iterations=1) 42 | img_erosion = cv2.erode(img_binary, kernel, iterations=1) 43 | print("Morphological Operations: Applied Dilation and Erosion") 44 | 45 | # %% [8. Visualization] 46 | plt.figure(figsize=(15, 10)) 47 | plt.subplot(2, 4, 1) 48 | plt.title("Original") 49 | plt.imshow(img_gray, cmap="gray") 50 | plt.subplot(2, 4, 2) 51 | plt.title("Gaussian Blur") 52 | plt.imshow(img_gaussian, cmap="gray") 53 | plt.subplot(2, 4, 3) 54 | plt.title("Median Blur") 55 | plt.imshow(img_median, cmap="gray") 56 | plt.subplot(2, 4, 4) 57 | plt.title("Sharpened") 58 | plt.imshow(img_sharpened, cmap="gray") 59 | plt.subplot(2, 4, 5) 60 | plt.title("Sobel") 61 | plt.imshow(img_sobel, cmap="gray") 62 | plt.subplot(2, 4, 6) 63 | plt.title("Canny") 64 | plt.imshow(img_canny, cmap="gray") 65 | plt.subplot(2, 4, 7) 66 | plt.title("Binary Threshold") 67 | plt.imshow(img_binary, cmap="gray") 68 | plt.subplot(2, 4, 8) 69 | plt.title("Dilation") 70 | plt.imshow(img_dilation, cmap="gray") 71 | plt.savefig("image_filtering_output.png") 72 | print("Visualization: Filters saved as image_filtering_output.png") 73 | 74 | # %% [9. Interview Scenario: Image Filtering] 75 | """ 76 | Interview Scenario: Image Filtering 77 | Q: How does Canny edge detection work, and what are its parameters? 78 | A: Canny uses Gaussian blur, gradient computation, non-maximum suppression, and hysteresis thresholding. 79 | Key: Parameters are low/high thresholds for edge strength. 80 | Example: cv2.Canny(img, 100, 200) 81 | """ 82 | 83 | # Execute the demo 84 | if __name__ == "__main__": 85 | run_image_filtering_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/04 Object_Detection_and_Recognition/03 Deep_Learning_Based_Detection/deep_learning_detection.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Deep Learning-Based Detection] 2 | # Learn YOLOv3 object detection with OpenCV and TensorFlow. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | # Download: yolov3.weights, yolov3.cfg, coco.names (see README) 6 | import cv2 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | 10 | def run_deep_learning_detection_demo(): 11 | # %% [2. Create Synthetic Image] 12 | # Generate a 300x300 image with a car-like shape (rectangle + wheels) 13 | img = np.zeros((300, 300, 3), dtype=np.uint8) 14 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car body 15 | cv2.circle(img, (120, 200), 10, (0, 0, 0), -1) # Left wheel 16 | cv2.circle(img, (180, 200), 10, (0, 0, 0), -1) # Right wheel 17 | print("Synthetic Image: 300x300 with car-like shape created") 18 | 19 | # %% [3. Load YOLOv3 Model] 20 | net = cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights") 21 | layer_names = net.getLayerNames() 22 | output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()] 23 | with open("coco.names", "r") as f: 24 | classes = [line.strip() for line in f.readlines()] 25 | print("YOLOv3: Model and COCO classes loaded") 26 | 27 | # %% [4. Preprocess and Detect Objects] 28 | blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False) 29 | net.setInput(blob) 30 | outs = net.forward(output_layers) 31 | 32 | height, width = img.shape[:2] 33 | boxes = [] 34 | confidences = [] 35 | class_ids = [] 36 | 37 | for out in outs: 38 | for detection in out: 39 | scores = detection[5:] 40 | class_id = np.argmax(scores) 41 | confidence = scores[class_id] 42 | if confidence > 0.5 and class_id == 2: # Class 2 is 'car' in COCO 43 | center_x = int(detection[0] * width) 44 | center_y = int(detection[1] * height) 45 | w = int(detection[2] * width) 46 | h = int(detection[3] * height) 47 | x = int(center_x - w / 2) 48 | y = int(center_y - h / 2) 49 | boxes.append([x, y, w, h]) 50 | confidences.append(float(confidence)) 51 | class_ids.append(class_id) 52 | 53 | indices = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) 54 | img_yolo = img.copy() 55 | for i in indices: 56 | box = boxes[i] 57 | x, y, w, h = box 58 | label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}" 59 | cv2.rectangle(img_yolo, (x, y), (x+w, y+h), (0, 0, 255), 2) 60 | cv2.putText(img_yolo, label, (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 61 | print(f"YOLOv3 Detection: {len(indices)} objects detected (car-like shape)") 62 | 63 | # %% [5. Visualization] 64 | plt.figure(figsize=(10, 5)) 65 | plt.subplot(1, 2, 1) 66 | plt.title("Original Image") 67 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 68 | plt.subplot(1, 2, 2) 69 | plt.title("YOLOv3 Detection") 70 | plt.imshow(cv2.cvtColor(img_yolo, cv2.COLOR_BGR2RGB)) 71 | plt.savefig("deep_learning_detection_output.png") 72 | print("Visualization: YOLOv3 results saved as deep_learning_detection_output.png") 73 | 74 | # %% [6. Interview Scenario: Deep Learning-Based Detection] 75 | """ 76 | Interview Scenario: Deep Learning-Based Detection 77 | Q: Compare YOLO, SSD, and Faster R-CNN for object detection. 78 | A: YOLO is fast, single-pass, ideal for real-time; SSD balances speed and accuracy; Faster R-CNN is accurate but slower due to region proposals. 79 | Key: YOLOv3 uses a single network with grid-based predictions. 80 | Example: cv2.dnn.readNetFromDarknet("yolov3.cfg", "yolov3.weights") 81 | """ 82 | 83 | # Execute the demo 84 | if __name__ == "__main__": 85 | run_deep_learning_detection_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/02 Image_Segmentation/image_segmentation.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Segmentation] 2 | # Learn contour detection, watershed, GrabCut, and K-Means clustering with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_segmentation_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 image with overlapping shapes 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | cv2.circle(img, (150, 100), 50, (255, 0, 0), -1) # Blue circle 14 | cv2.rectangle(img, (100, 150, 100, 100), (0, 255, 0), -1) # Green rectangle 15 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | print("Synthetic Image: 300x300 with blue circle and green rectangle created") 17 | 18 | # %% [3. Contour Detection] 19 | _, thresh = cv2.threshold(img_gray, 1, 255, cv2.THRESH_BINARY) 20 | contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 21 | img_contours = img.copy() 22 | cv2.drawContours(img_contours, contours, -1, (0, 0, 255), 2) 23 | print(f"Contour Detection: {len(contours)} contours found") 24 | 25 | # %% [4. Watershed Algorithm] 26 | dist_transform = cv2.distanceTransform(thresh, cv2.DIST_L2, 5) 27 | _, sure_fg = cv2.threshold(dist_transform, 0.7 * dist_transform.max(), 255, 0) 28 | sure_fg = np.uint8(sure_fg) 29 | unknown = cv2.subtract(thresh, sure_fg) 30 | _, markers = cv2.connectedComponents(sure_fg) 31 | markers = markers + 1 32 | markers[unknown == 255] = 0 33 | img_watershed = img.copy() 34 | markers = cv2.watershed(img_watershed, markers) 35 | img_watershed[markers == -1] = [255, 0, 0] 36 | print("Watershed Algorithm: Segmented overlapping regions") 37 | 38 | # %% [5. GrabCut] 39 | mask = np.zeros(img.shape[:2], np.uint8) 40 | bgdModel = np.zeros((1, 65), np.float64) 41 | fgdModel = np.zeros((1, 65), np.float64) 42 | rect = (50, 50, 200, 200) 43 | cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) 44 | mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8') 45 | img_grabcut = img * mask2[:, :, np.newaxis] 46 | print("GrabCut: Foreground segmented with rectangular initialization") 47 | 48 | # %% [6. K-Means Clustering] 49 | Z = img.reshape((-1, 3)).astype(np.float32) 50 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 51 | K = 3 52 | _, labels, centers = cv2.kmeans(Z, K, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS) 53 | centers = np.uint8(centers) 54 | img_kmeans = centers[labels.flatten()].reshape(img.shape) 55 | print(f"K-Means Clustering: Segmented into {K} colors") 56 | 57 | # %% [7. Visualization] 58 | plt.figure(figsize=(15, 8)) 59 | plt.subplot(2, 3, 1) 60 | plt.title("Original") 61 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 62 | plt.subplot(2, 3, 2) 63 | plt.title("Contours") 64 | plt.imshow(cv2.cvtColor(img_contours, cv2.COLOR_BGR2RGB)) 65 | plt.subplot(2, 3, 3) 66 | plt.title("Watershed") 67 | plt.imshow(cv2.cvtColor(img_watershed, cv2.COLOR_BGR2RGB)) 68 | plt.subplot(2, 3, 4) 69 | plt.title("GrabCut") 70 | plt.imshow(cv2.cvtColor(img_grabcut, cv2.COLOR_BGR2RGB)) 71 | plt.subplot(2, 3, 5) 72 | plt.title("K-Means") 73 | plt.imshow(cv2.cvtColor(img_kmeans, cv2.COLOR_BGR2RGB)) 74 | plt.savefig("image_segmentation_output.png") 75 | print("Visualization: Segmentation results saved as image_segmentation_output.png") 76 | 77 | # %% [8. Interview Scenario: Image Segmentation] 78 | """ 79 | Interview Scenario: Image Segmentation 80 | Q: How does GrabCut work for image segmentation? 81 | A: GrabCut uses graph cuts to separate foreground and background, initialized with a rectangle or mask. 82 | Key: Iteratively refines segmentation using Gaussian Mixture Models. 83 | Example: cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5) 84 | """ 85 | 86 | # Execute the demo 87 | if __name__ == "__main__": 88 | run_image_segmentation_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/01 Performance_Optimization/performance_optimization.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Performance Optimization] 2 | # Learn parallel processing, GPU acceleration, memory management, and algorithm efficiency with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib multiprocess 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | from multiprocess import Pool 9 | import time 10 | 11 | def process_image(img): 12 | """Helper function to process an image (e.g., edge detection).""" 13 | return cv2.Canny(img, 100, 200) 14 | 15 | def run_performance_optimization_demo(): 16 | # %% [2. Create Synthetic Images] 17 | # Generate 10 synthetic 300x300 images with a white square 18 | images = [] 19 | for _ in range(10): 20 | img = np.zeros((300, 300), dtype=np.uint8) 21 | img[100:200, 100:200] = 255 # White square 22 | images.append(img) 23 | print("Synthetic Images: 10 images with white squares created") 24 | 25 | # %% [3. Parallel Processing] 26 | start_time = time.time() 27 | with Pool() as pool: 28 | results_parallel = pool.map(process_image, images) 29 | parallel_time = time.time() - start_time 30 | print(f"Parallel Processing: Processed {len(images)} images in {parallel_time:.2f} seconds") 31 | 32 | # %% [4. Sequential Processing (for Comparison)] 33 | start_time = time.time() 34 | results_sequential = [process_image(img) for img in images] 35 | sequential_time = time.time() - start_time 36 | print(f"Sequential Processing: Processed {len(images)} images in {sequential_time:.2f} seconds") 37 | 38 | # %% [5. GPU Acceleration (Simulated)] 39 | # Note: Requires OpenCV with CUDA support (not standard opencv-python) 40 | # Simulated by describing CUDA-based Canny edge detection 41 | img_cuda = images[0].copy() # Simulate single image processing 42 | # In real CUDA setup: cv2.cuda_GpuMat, cv2.cuda.Canny 43 | img_cuda_canny = cv2.Canny(img_cuda, 100, 200) # CPU fallback for demo 44 | print("GPU Acceleration: Simulated CUDA-based Canny edge detection") 45 | # Real CUDA setup: Install opencv-python with CUDA (e.g., via opencv-contrib-python with CUDA build) 46 | 47 | # %% [6. Memory Management] 48 | # Optimize memory by reusing buffers and reducing copies 49 | img_memory = images[0].copy() 50 | # Pre-allocate output buffer 51 | output_buffer = np.zeros_like(img_memory) 52 | cv2.Canny(img_memory, 100, 200, dst=output_buffer) 53 | print("Memory Management: Used pre-allocated buffer for Canny edge detection") 54 | 55 | # %% [7. Algorithm Efficiency] 56 | # Compare efficient vs. naive edge detection 57 | start_time = time.time() 58 | img_efficient = cv2.Canny(images[0], 100, 200) # Efficient OpenCV Canny 59 | efficient_time = time.time() - start_time 60 | print(f"Algorithm Efficiency: Efficient Canny took {efficient_time:.2f} seconds") 61 | 62 | # %% [8. Visualization] 63 | plt.figure(figsize=(15, 5)) 64 | plt.subplot(1, 3, 1) 65 | plt.title("Original Image") 66 | plt.imshow(images[0], cmap="gray") 67 | plt.subplot(1, 3, 2) 68 | plt.title("Parallel Canny") 69 | plt.imshow(results_parallel[0], cmap="gray") 70 | plt.subplot(1, 3, 3) 71 | plt.title("Memory Optimized Canny") 72 | plt.imshow(output_buffer, cmap="gray") 73 | plt.savefig("performance_optimization_output.png") 74 | print("Visualization: Optimization results saved as performance_optimization_output.png") 75 | 76 | # %% [9. Interview Scenario: Performance Optimization] 77 | """ 78 | Interview Scenario: Performance Optimization 79 | Q: How do you optimize OpenCV for real-time performance? 80 | A: Use parallel processing (e.g., multiprocess.Pool), GPU acceleration (CUDA), memory management (pre-allocated buffers), and efficient algorithms (e.g., OpenCV's optimized Canny). 81 | Key: Profile bottlenecks with tools like cProfile and prioritize GPU for compute-heavy tasks. 82 | Example: pool.map(process_image, images) 83 | """ 84 | 85 | # Execute the demo 86 | if __name__ == "__main__": 87 | run_performance_optimization_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/01 Core_Image_Processing/02 Image_Transformations/image_transformations.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Image Transformations] 2 | # Learn to resize, crop, rotate, flip, and apply affine/perspective transformations with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_image_transformations_demo(): 10 | # %% [2. Create Synthetic Image] 11 | # Generate a 300x300 RGB image with a green rectangle 12 | img = np.zeros((300, 300, 3), dtype=np.uint8) 13 | img[100:200, 100:200] = [0, 255, 0] # Green square (BGR format) 14 | print("Synthetic Image: 300x300 RGB with green square created") 15 | 16 | # %% [3. Resizing] 17 | img_resized = cv2.resize(img, (150, 150), interpolation=cv2.INTER_LINEAR) 18 | print("Resizing: Image resized to 150x150") 19 | 20 | # %% [4. Cropping] 21 | img_cropped = img[50:250, 50:250] 22 | print("Cropping: Image cropped to 200x200") 23 | 24 | # %% [5. Rotation] 25 | center = (img.shape[1]//2, img.shape[0]//2) 26 | matrix = cv2.getRotationMatrix2D(center, 45, 1.0) 27 | img_rotated = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 28 | print("Rotation: Image rotated by 45 degrees") 29 | 30 | # %% [6. Flipping] 31 | img_flipped = cv2.flip(img, 1) # Horizontal flip 32 | print("Flipping: Image flipped horizontally") 33 | 34 | # %% [7. Translation] 35 | matrix = np.float32([[1, 0, 50], [0, 1, 50]]) # Shift 50px right, 50px down 36 | img_translated = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 37 | print("Translation: Image shifted 50px right and down") 38 | 39 | # %% [8. Affine Transformation] 40 | pts1 = np.float32([[50,50], [200,50], [50,200]]) 41 | pts2 = np.float32([[10,100], [200,50], [100,250]]) 42 | matrix = cv2.getAffineTransform(pts1, pts2) 43 | img_affine = cv2.warpAffine(img, matrix, (img.shape[1], img.shape[0])) 44 | print("Affine Transformation: Image skewed") 45 | 46 | # %% [9. Perspective Transformation] 47 | pts1 = np.float32([[50,50], [250,50], [50,250], [250,250]]) 48 | pts2 = np.float32([[0,0], [300,0], [50,300], [250,300]]) 49 | matrix = cv2.getPerspectiveTransform(pts1, pts2) 50 | img_perspective = cv2.warpPerspective(img, matrix, (img.shape[1], img.shape[0])) 51 | print("Perspective Transformation: Image warped") 52 | 53 | # %% [10. Visualization] 54 | plt.figure(figsize=(15, 10)) 55 | plt.subplot(2, 4, 1) 56 | plt.title("Original") 57 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 58 | plt.subplot(2, 4, 2) 59 | plt.title("Resized") 60 | plt.imshow(cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)) 61 | plt.subplot(2, 4, 3) 62 | plt.title("Cropped") 63 | plt.imshow(cv2.cvtColor(img_cropped, cv2.COLOR_BGR2RGB)) 64 | plt.subplot(2, 4, 4) 65 | plt.title("Rotated") 66 | plt.imshow(cv2.cvtColor(img_rotated, cv2.COLOR_BGR2RGB)) 67 | plt.subplot(2, 4, 5) 68 | plt.title("Flipped") 69 | plt.imshow(cv2.cvtColor(img_flipped, cv2.COLOR_BGR2RGB)) 70 | plt.subplot(2, 4, 6) 71 | plt.title("Translated") 72 | plt.imshow(cv2.cvtColor(img_translated, cv2.COLOR_BGR2RGB)) 73 | plt.subplot(2, 4, 7) 74 | plt.title("Affine") 75 | plt.imshow(cv2.cvtColor(img_affine, cv2.COLOR_BGR2RGB)) 76 | plt.subplot(2, 4, 8) 77 | plt.title("Perspective") 78 | plt.imshow(cv2.cvtColor(img_perspective, cv2.COLOR_BGR2RGB)) 79 | plt.savefig("image_transformations_output.png") 80 | print("Visualization: Transformations saved as image_transformations_output.png") 81 | 82 | # %% [11. Interview Scenario: Image Transformations] 83 | """ 84 | Interview Scenario: Image Transformations 85 | Q: How do you rotate an image in OpenCV, and what’s the role of the rotation matrix? 86 | A: Use cv2.getRotationMatrix2D to create a 2D rotation matrix, then apply it with cv2.warpAffine. 87 | Key: The matrix defines angle, center, and scale for precise rotation. 88 | Example: cv2.getRotationMatrix2D(center, 45, 1.0) 89 | """ 90 | 91 | # Execute the demo 92 | if __name__ == "__main__": 93 | run_image_transformations_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/03 Integration/integration.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Integration] 2 | # Learn OpenCV integration with TensorFlow, ROS, and Raspberry Pi. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | import tensorflow as tf 9 | from tensorflow.keras.applications import MobileNetV2 10 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 11 | 12 | def run_integration_demo(): 13 | # %% [2. Create Synthetic Image] 14 | img = np.zeros((300, 300, 3), dtype=np.uint8) 15 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car-like shape 16 | print("Synthetic Image: 300x300 with car-like shape created") 17 | 18 | # %% [3. OpenCV with TensorFlow] 19 | model = MobileNetV2(weights='imagenet') 20 | img_resized = cv2.resize(img, (224, 224)) 21 | img_array = np.expand_dims(img_resized, axis=0) 22 | img_preprocessed = preprocess_input(img_array) 23 | predictions = model.predict(img_array) 24 | decoded_preds = decode_predictions(predictions, top=1)[0] 25 | img_tf = img.copy() 26 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 27 | cv2.putText(img_tf, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 28 | print(f"OpenCV with TensorFlow: Predicted {decoded_preds[0][1]} ({decoded_preds[0][2]:.2f})") 29 | 30 | # %% [4. OpenCV with ROS (Simulated)] 31 | """ 32 | ROS Node Example (Conceptual): 33 | import rospy 34 | from sensor_msgs.msg import Image 35 | from cv_bridge import CvBridge 36 | def image_callback(msg): 37 | bridge = CvBridge() 38 | cv_image = bridge.imgmsg_to_cv2(msg, "bgr8") 39 | edges = cv2.Canny(cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY), 100, 200) 40 | pub.publish(bridge.cv2_to_imgmsg(edges, "mono8")) 41 | rospy.init_node('vision_node') 42 | pub = rospy.Publisher('edges', Image, queue_size=10) 43 | sub = rospy.Subscriber('camera/image', Image, image_callback) 44 | rospy.spin() 45 | """ 46 | img_ros = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 200) 47 | print("OpenCV with ROS: Simulated ROS node for edge detection") 48 | 49 | # %% [5. OpenCV in Embedded Systems (Raspberry Pi, Conceptual)] 50 | """ 51 | Raspberry Pi Setup: 52 | 1. Install Raspbian and Python 3.8+. 53 | 2. Install OpenCV: pip install opencv-python. 54 | 3. Connect camera module. 55 | 4. Run lightweight vision pipeline: 56 | cap = cv2.VideoCapture(0) 57 | while True: 58 | ret, frame = cap.read() 59 | edges = cv2.Canny(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), 100, 200) 60 | cv2.imshow('Edges', edges) 61 | if cv2.waitKey(1) & 0xFF == ord('q'): 62 | break 63 | cap.release() 64 | cv2.destroyAllWindows() 65 | """ 66 | img_pi = cv2.Canny(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 100, 200) 67 | print("OpenCV in Raspberry Pi: Simulated lightweight edge detection") 68 | 69 | # %% [6. Visualization] 70 | plt.figure(figsize=(15, 5)) 71 | plt.subplot(1, 3, 1) 72 | plt.title("TensorFlow Integration") 73 | plt.imshow(cv2.cvtColor(img_tf, cv2.COLOR_BGR2RGB)) 74 | plt.subplot(1, 3, 2) 75 | plt.title("ROS Integration (Simulated)") 76 | plt.imshow(img_ros, cmap="gray") 77 | plt.subplot(1, 3, 3) 78 | plt.title("Raspberry Pi (Simulated)") 79 | plt.imshow(img_pi, cmap="gray") 80 | plt.savefig("integration_output.png") 81 | print("Visualization: Integration results saved as integration_output.png") 82 | 83 | # %% [7. Interview Scenario: Integration] 84 | """ 85 | Interview Scenario: Integration 86 | Q: How do you integrate OpenCV with TensorFlow for real-time inference? 87 | A: Use OpenCV for image preprocessing (e.g., resize, color conversion) and TensorFlow for model inference (e.g., MobileNetV2). 88 | Key: Ensure compatible data formats (e.g., BGR to RGB, normalized inputs). 89 | Example: cv2.resize(img, (224, 224)); model.predict(img_array) 90 | """ 91 | 92 | # Execute the demo 93 | if __name__ == "__main__": 94 | run_integration_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/02 Video_Analysis/video_analysis.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Video Analysis] 2 | # Learn frame differencing, background subtraction, motion detection, and stabilization with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_video_analysis_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | frames_gray = [cv2.cvtColor(f, cv2.COLOR_BGR2GRAY) for f in frames] 19 | print("Synthetic Video: 10 frames with moving white square created") 20 | 21 | # %% [3. Frame Differencing] 22 | diff = cv2.absdiff(frames_gray[4], frames_gray[5]) 23 | _, diff_thresh = cv2.threshold(diff, 30, 255, cv2.THRESH_BINARY) 24 | print("Frame Differencing: Motion detected between frames 5 and 6") 25 | 26 | # %% [4. Background Subtraction (MOG2)] 27 | bg_subtractor_mog2 = cv2.createBackgroundSubtractorMOG2() 28 | mog2_mask = bg_subtractor_mog2.apply(frames[5]) 29 | print("Background Subtraction: MOG2 applied to frame 6") 30 | 31 | # %% [5. Background Subtraction (KNN)] 32 | bg_subtractor_knn = cv2.createBackgroundSubtractorKNN() 33 | knn_mask = bg_subtractor_knn.apply(frames[5]) 34 | print("Background Subtraction: KNN applied to frame 6") 35 | 36 | # %% [6. Motion Detection] 37 | contours, _ = cv2.findContours(mog2_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) 38 | img_motion = frames[5].copy() 39 | for contour in contours: 40 | if cv2.contourArea(contour) > 100: 41 | x, y, w, h = cv2.boundingRect(contour) 42 | cv2.rectangle(img_motion, (x, y), (x+w, y+h), (0, 0, 255), 2) 43 | print(f"Motion Detection: {len(contours)} moving objects detected") 44 | 45 | # %% [7. Video Stabilization (Simplified)] 46 | # Simulate stabilization by aligning frame 6 to frame 5 using translation 47 | orb = cv2.ORB_create() 48 | kp1, des1 = orb.detectAndCompute(frames_gray[4], None) 49 | kp2, des2 = orb.detectAndCompute(frames_gray[5], None) 50 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 51 | matches = bf.match(des1, des2) 52 | src_pts = np.float32([kp1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) 53 | dst_pts = np.float32([kp2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) 54 | M, _ = cv2.estimateAffinePartial2D(src_pts, dst_pts) 55 | img_stabilized = cv2.warpAffine(frames[5], M, (300, 300)) 56 | print("Video Stabilization: Frame 6 aligned to frame 5") 57 | 58 | # %% [8. Visualization] 59 | plt.figure(figsize=(15, 8)) 60 | plt.subplot(2, 3, 1) 61 | plt.title("Frame 6") 62 | plt.imshow(cv2.cvtColor(frames[5], cv2.COLOR_BGR2RGB)) 63 | plt.subplot(2, 3, 2) 64 | plt.title("Frame Diff") 65 | plt.imshow(diff_thresh, cmap="gray") 66 | plt.subplot(2, 3, 3) 67 | plt.title("MOG2 Mask") 68 | plt.imshow(mog2_mask, cmap="gray") 69 | plt.subplot(2, 3, 4) 70 | plt.title("KNN Mask") 71 | plt.imshow(knn_mask, cmap="gray") 72 | plt.subplot(2, 3, 5) 73 | plt.title("Motion Detection") 74 | plt.imshow(cv2.cvtColor(img_motion, cv2.COLOR_BGR2RGB)) 75 | plt.subplot(2, 3, 6) 76 | plt.title("Stabilized") 77 | plt.imshow(cv2.cvtColor(img_stabilized, cv2.COLOR_BGR2RGB)) 78 | plt.savefig("video_analysis_output.png") 79 | print("Visualization: Analysis results saved as video_analysis_output.png") 80 | 81 | # %% [9. Interview Scenario: Video Analysis] 82 | """ 83 | Interview Scenario: Video Analysis 84 | Q: How does MOG2 background subtraction work? 85 | A: MOG2 models each pixel as a mixture of Gaussians to separate foreground from background. 86 | Key: Robust to lighting changes and dynamic backgrounds. 87 | Example: cv2.createBackgroundSubtractorMOG2() 88 | """ 89 | 90 | # Execute the demo 91 | if __name__ == "__main__": 92 | run_video_analysis_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/05 Optimization_and_Deployment/02 Deployment/deployment.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Deployment] 2 | # Learn Flask for web apps, Docker containers, and real-time inference with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib tensorflow flask 5 | # Optional: Install Docker for containerization 6 | import cv2 7 | import numpy as np 8 | import matplotlib.pyplot as plt 9 | from flask import Flask, request, jsonify 10 | import tensorflow as tf 11 | from tensorflow.keras.applications import MobileNetV2 12 | from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions 13 | import base64 14 | from io import BytesIO 15 | 16 | app = Flask(__name__) 17 | 18 | def run_deployment_demo(): 19 | # %% [2. Load Pretrained Model] 20 | model = MobileNetV2(weights='imagenet') 21 | print("Deployment: Loaded MobileNetV2 for real-time inference") 22 | 23 | # %% [3. Create Synthetic Image] 24 | img = np.zeros((300, 300, 3), dtype=np.uint8) 25 | cv2.rectangle(img, (100, 150), (200, 200), (255, 255, 255), -1) # Car-like shape 26 | print("Synthetic Image: 300x300 with car-like shape created") 27 | 28 | # %% [4. Flask App for Real-Time Inference] 29 | @app.route('/predict', methods=['POST']) 30 | def predict(): 31 | data = request.json 32 | img_data = base64.b64decode(data['image']) 33 | img_array = np.frombuffer(img_data, np.uint8) 34 | img = cv2.imdecode(img_array, cv2.IMREAD_COLOR) 35 | img_resized = cv2.resize(img, (224, 224)) 36 | img_array = np.expand_dims(img_resized, axis=0) 37 | img_preprocessed = preprocess_input(img_array) 38 | predictions = model.predict(img_array) 39 | decoded_preds = decode_predictions(predictions, top=1)[0] 40 | return jsonify({'prediction': decoded_preds[0][1], 'confidence': float(decoded_preds[0][2])}) 41 | 42 | # Simulate Flask inference with synthetic image 43 | _, img_encoded = cv2.imencode('.png', img) 44 | img_base64 = base64.b64encode(img_encoded).decode('utf-8') 45 | simulated_request = {'image': img_base64} 46 | with app.test_request_context('/predict', json=simulated_request, method='POST'): 47 | response = predict() 48 | print(f"Flask Inference: Simulated prediction: {response.json['prediction']} ({response.json['confidence']:.2f})") 49 | 50 | # %% [5. Docker Container (Conceptual)] 51 | """ 52 | Dockerfile Example: 53 | FROM python:3.8-slim 54 | WORKDIR /app 55 | COPY requirements.txt . 56 | RUN pip install -r requirements.txt 57 | COPY . . 58 | CMD ["flask", "run", "--host=0.0.0.0"] 59 | 60 | Build and Run: 61 | docker build -t vision-app . 62 | docker run -p 5000:5000 vision-app 63 | """ 64 | print("Docker: Conceptual Dockerfile provided for containerizing Flask app") 65 | 66 | # %% [6. Real-Time Inference] 67 | start_time = time.time() 68 | img_resized = cv2.resize(img, (224, 224)) 69 | img_array = np.expand_dims(img_resized, axis=0) 70 | img_preprocessed = preprocess_input(img_array) 71 | predictions = model.predict(img_array) 72 | inference_time = time.time() - start_time 73 | decoded_preds = decode_predictions(predictions, top=1)[0] 74 | img_pred = img.copy() 75 | label = f"{decoded_preds[0][1]}: {decoded_preds[0][2]:.2f}" 76 | cv2.putText(img_pred, label, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) 77 | print(f"Real-Time Inference: Predicted {decoded_preds[0][1]} in {inference_time:.2f} seconds") 78 | 79 | # %% [7. Visualization] 80 | plt.figure(figsize=(10, 5)) 81 | plt.subplot(1, 2, 1) 82 | plt.title("Original Image") 83 | plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) 84 | plt.subplot(1, 2, 2) 85 | plt.title("Inference Result") 86 | plt.imshow(cv2.cvtColor(img_pred, cv2.COLOR_BGR2RGB)) 87 | plt.savefig("deployment_output.png") 88 | print("Visualization: Deployment results saved as deployment_output.png") 89 | 90 | # %% [8. Interview Scenario: Deployment] 91 | """ 92 | Interview Scenario: Deployment 93 | Q: What are the benefits of using Docker for deploying vision applications? 94 | A: Docker ensures consistent environments, simplifies dependency management, and enables scalability. 95 | Key: Containers isolate the app, making it portable across platforms (e.g., AWS, GCP). 96 | Example: docker build -t vision-app . 97 | """ 98 | 99 | # Execute the demo 100 | if __name__ == "__main__": 101 | run_deployment_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/02 Advanced_Image_Processing/01 Feature_Detection/feature_detection.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Feature Detection] 2 | # Learn corner detection, keypoint detection, feature matching, and homography with OpenCV. 3 | 4 | # Setup: pip install opencv-python opencv-contrib-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_feature_detection_demo(): 10 | # %% [2. Create Synthetic Images] 11 | # Generate a 300x300 image with a checkerboard pattern 12 | img1 = np.zeros((300, 300, 3), dtype=np.uint8) 13 | for i in range(0, 300, 50): 14 | for j in range(0, 300, 50): 15 | if (i//50 + j//50) % 2 == 0: 16 | img1[i:i+50, j:j+50] = [255, 255, 255] 17 | # Create a rotated version 18 | center = (img1.shape[1]//2, img1.shape[0]//2) 19 | matrix = cv2.getRotationMatrix2D(center, 10, 1.0) 20 | img2 = cv2.warpAffine(img1, matrix, (img1.shape[1], img1.shape[0])) 21 | img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) 22 | img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) 23 | print("Synthetic Images: Checkerboard and rotated version created") 24 | 25 | # %% [3. Corner Detection (Harris)] 26 | harris = cv2.cornerHarris(img1_gray, blockSize=2, ksize=3, k=0.04) 27 | img1_harris = img1.copy() 28 | img1_harris[harris > 0.01 * harris.max()] = [0, 0, 255] 29 | print("Harris Corner Detection: Corners marked in red") 30 | 31 | # %% [4. Corner Detection (Shi-Tomasi)] 32 | corners = cv2.goodFeaturesToTrack(img1_gray, maxCorners=50, qualityLevel=0.01, minDistance=10) 33 | img1_shi = img1.copy() 34 | for corner in corners: 35 | x, y = corner.ravel() 36 | cv2.circle(img1_shi, (int(x), int(y)), 3, (0, 255, 0), -1) 37 | print("Shi-Tomasi Corner Detection: Corners marked in green") 38 | 39 | # %% [5. Keypoint Detection (ORB)] 40 | orb = cv2.ORB_create() 41 | keypoints1, descriptors1 = orb.detectAndCompute(img1_gray, None) 42 | keypoints2, descriptors2 = orb.detectAndCompute(img2_gray, None) 43 | img1_orb = cv2.drawKeypoints(img1, keypoints1, None, color=(0, 255, 0)) 44 | print(f"ORB Keypoints: {len(keypoints1)} keypoints detected in first image") 45 | 46 | # %% [6. Feature Matching (Brute-Force)] 47 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 48 | matches = bf.match(descriptors1, descriptors2) 49 | matches = sorted(matches, key=lambda x: x.distance) 50 | img_matches = cv2.drawMatches(img1, keypoints1, img2, keypoints2, matches[:10], None, flags=2) 51 | print("Brute-Force Matching: Top 10 matches drawn") 52 | 53 | # %% [7. Homography Estimation] 54 | src_pts = np.float32([keypoints1[m.queryIdx].pt for m in matches]).reshape(-1, 1, 2) 55 | dst_pts = np.float32([keypoints2[m.trainIdx].pt for m in matches]).reshape(-1, 1, 2) 56 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) 57 | h, w = img1_gray.shape 58 | pts = np.float32([[0, 0], [0, h-1], [w-1, h-1], [w-1, 0]]).reshape(-1, 1, 2) 59 | dst = cv2.perspectiveTransform(pts, M) 60 | img2_homography = img2.copy() 61 | cv2.polylines(img2_homography, [np.int32(dst)], True, (0, 0, 255), 3) 62 | print("Homography Estimation: Transformation outline drawn") 63 | 64 | # %% [8. Visualization] 65 | plt.figure(figsize=(15, 10)) 66 | plt.subplot(2, 3, 1) 67 | plt.title("Original Image 1") 68 | plt.imshow(cv2.cvtColor(img1, cv2.COLOR_BGR2RGB)) 69 | plt.subplot(2, 3, 2) 70 | plt.title("Harris Corners") 71 | plt.imshow(cv2.cvtColor(img1_harris, cv2.COLOR_BGR2RGB)) 72 | plt.subplot(2, 3, 3) 73 | plt.title("Shi-Tomasi Corners") 74 | plt.imshow(cv2.cvtColor(img1_shi, cv2.COLOR_BGR2RGB)) 75 | plt.subplot(2, 3, 4) 76 | plt.title("ORB Keypoints") 77 | plt.imshow(cv2.cvtColor(img1_orb, cv2.COLOR_BGR2RGB)) 78 | plt.subplot(2, 3, 5) 79 | plt.title("Feature Matches") 80 | plt.imshow(cv2.cvtColor(img_matches, cv2.COLOR_BGR2RGB)) 81 | plt.subplot(2, 3, 6) 82 | plt.title("Homography") 83 | plt.imshow(cv2.cvtColor(img2_homography, cv2.COLOR_BGR2RGB)) 84 | plt.savefig("feature_detection_output.png") 85 | print("Visualization: Feature detection results saved as feature_detection_output.png") 86 | 87 | # %% [9. Interview Scenario: Feature Detection] 88 | """ 89 | Interview Scenario: Feature Detection 90 | Q: What’s the difference between SIFT and ORB for keypoint detection? 91 | A: SIFT is scale-invariant and robust but patented and slower; ORB is fast, open-source, and good for real-time. 92 | Key: ORB uses binary descriptors, suitable for Hamming distance matching. 93 | Example: cv2.ORB_create() 94 | """ 95 | 96 | # Execute the demo 97 | if __name__ == "__main__": 98 | run_feature_detection_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/03 Object_Tracking/object_tracking.py: -------------------------------------------------------------------------------- 1 | # %% [1. Introduction to Object Tracking] 2 | # Learn MeanShift, CamShift, KCF, and CSRT trackers with OpenCV. 3 | 4 | # Setup: pip install opencv-python numpy matplotlib 5 | import cv2 6 | import numpy as np 7 | import matplotlib.pyplot as plt 8 | 9 | def run_object_tracking_demo(): 10 | # %% [2. Create Synthetic Video Frames] 11 | # Generate 10 frames (300x300) with a moving white square 12 | frames = [] 13 | for i in range(10): 14 | frame = np.zeros((300, 300, 3), dtype=np.uint8) 15 | x = 100 + i * 10 # Move 10px right per frame 16 | frame[x:x+50, 100:150] = [255, 255, 255] # White square 17 | frames.append(frame) 18 | print("Synthetic Video: 10 frames with moving white square created") 19 | 20 | # %% [3. Initialize ROI] 21 | roi = (100, 100, 50, 50) # (x, y, w, h) for first frame 22 | x, y, w, h = roi 23 | frame_roi = frames[0][y:y+h, x:x+w] 24 | hsv_roi = cv2.cvtColor(frame_roi, cv2.COLOR_BGR2HSV) 25 | roi_hist = cv2.calcHist([hsv_roi], [0], None, [180], [0, 180]) 26 | cv2.normalize(roi_hist, roi_hist, 0, 255, cv2.NORM_MINMAX) 27 | 28 | # %% [4. MeanShift Tracking] 29 | term_crit = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1) 30 | mean_shift_results = [] 31 | track_window = roi 32 | for frame in frames: 33 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 34 | dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) 35 | ret, track_window = cv2.meanShift(dst, track_window, term_crit) 36 | x, y, w, h = track_window 37 | img_ms = frame.copy() 38 | cv2.rectangle(img_ms, (x, y), (x+w, y+h), (0, 0, 255), 2) 39 | mean_shift_results.append(img_ms) 40 | print("MeanShift Tracking: Tracked square across frames") 41 | 42 | # %% [5. CamShift Tracking] 43 | cam_shift_results = [] 44 | track_window = roi 45 | for frame in frames: 46 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 47 | dst = cv2.calcBackProject([hsv], [0], roi_hist, [0, 180], 1) 48 | ret, track_window = cv2.CamShift(dst, track_window, term_crit) 49 | pts = cv2.boxPoints(ret) 50 | pts = np.int0(pts) 51 | img_cs = frame.copy() 52 | cv2.polylines(img_cs, [pts], True, (0, 255, 0), 2) 53 | cam_shift_results.append(img_cs) 54 | print("CamShift Tracking: Tracked square with adaptive size") 55 | 56 | # %% [6. KCF Tracking] 57 | kcf_tracker = cv2.TrackerKCF_create() 58 | kcf_results = [] 59 | kcf_tracker.init(frames[0], roi) 60 | for frame in frames: 61 | ok, bbox = kcf_tracker.update(frame) 62 | img_kcf = frame.copy() 63 | if ok: 64 | x, y, w, h = [int(v) for v in bbox] 65 | cv2.rectangle(img_kcf, (x, y), (x+w, y+h), (255, 0, 0), 2) 66 | kcf_results.append(img_kcf) 67 | print("KCF Tracking: Tracked square with KCF") 68 | 69 | # %% [7. CSRT Tracking] 70 | csrt_tracker = cv2.TrackerCSRT_create() 71 | csrt_results = [] 72 | csrt_tracker.init(frames[0], roi) 73 | for frame in frames: 74 | ok, bbox = csrt_tracker.update(frame) 75 | img_csrt = frame.copy() 76 | if ok: 77 | x, y, w, h = [int(v) for v in bbox] 78 | cv2.rectangle(img_csrt, (x, y), (x+w, y+h), (0, 255, 255), 2) 79 | csrt_results.append(img_csrt) 80 | print("CSRT Tracking: Tracked square with CSRT") 81 | 82 | # %% [8. Visualization] 83 | plt.figure(figsize=(15, 8)) 84 | for i, idx in enumerate([0, 4, 9]): # Show frames 1, 5, 10 85 | plt.subplot(4, 3, i+1) 86 | plt.title(f"MeanShift Frame {idx+1}") 87 | plt.imshow(cv2.cvtColor(mean_shift_results[idx], cv2.COLOR_BGR2RGB)) 88 | plt.subplot(4, 3, i+4) 89 | plt.title(f"CamShift Frame {idx+1}") 90 | plt.imshow(cv2.cvtColor(cam_shift_results[idx], cv2.COLOR_BGR2RGB)) 91 | plt.subplot(4, 3, i+7) 92 | plt.title(f"KCF Frame {idx+1}") 93 | plt.imshow(cv2.cvtColor(kcf_results[idx], cv2.COLOR_BGR2RGB)) 94 | plt.subplot(4, 3, i+10) 95 | plt.title(f"CSRT Frame {idx+1}") 96 | plt.imshow(cv2.cvtColor(csrt_results[idx], cv2.COLOR_BGR2RGB)) 97 | plt.tight_layout() 98 | plt.savefig("object_tracking_output.png") 99 | print("Visualization: Tracking results saved as object_tracking_output.png") 100 | 101 | # %% [9. Interview Scenario: Object Tracking] 102 | """ 103 | Interview Scenario: Object Tracking 104 | Q: What’s the difference between MeanShift and CSRT trackers? 105 | A: MeanShift tracks based on color histograms, fast but limited to fixed-size windows; CSRT uses discriminative correlation filters, more robust to scale and rotation. 106 | Key: CSRT is slower but more accurate for complex scenes. 107 | Example: cv2.TrackerCSRT_create() 108 | """ 109 | 110 | # Execute the demo 111 | if __name__ == "__main__": 112 | run_object_tracking_demo() -------------------------------------------------------------------------------- /Computer Vision Fundamentals/03 Video_Processing/README.md: -------------------------------------------------------------------------------- 1 | # 🎥 Video Processing with OpenCV (cv2) 2 | 3 |
Your guide to mastering video processing with OpenCV for AI/ML and computer vision interviews
10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Video Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`) and **Advanced Image Processing** (e.g., `optical_flow.py`) sections, focusing on handling video data for motion analysis and object tracking. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, and **Advanced Image Processing**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Video Basics**: Read/write videos, extract frames, and access properties (FPS, resolution). 20 | - **Video Analysis**: Perform frame differencing, background subtraction (MOG2, KNN), motion detection, and video stabilization. 21 | - **Object Tracking**: Track objects using MeanShift, CamShift, KCF, and CSRT trackers. 22 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic video frames. 23 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 24 | 25 | ## 🔍 Who Is This For? 26 | 27 | - Computer Vision Engineers advancing video processing skills. 28 | - Machine Learning Engineers exploring motion analysis. 29 | - AI Researchers mastering object tracking. 30 | - Software Engineers deepening computer vision expertise. 31 | - Anyone preparing for video processing interviews in AI/ML or retail. 32 | 33 | ## 🗺️ Learning Roadmap 34 | 35 | This section covers three key areas, each with a dedicated `.py` file: 36 | 37 | ### 📹 Video Basics (`video_basics.py`) 38 | - Reading/Writing Videos 39 | - Frame Extraction 40 | - Video Properties (FPS, Resolution) 41 | 42 | ### 🕒 Video Analysis (`video_analysis.py`) 43 | - Frame Differencing 44 | - Background Subtraction (MOG2, KNN) 45 | - Motion Detection 46 | - Video Stabilization 47 | 48 | ### 🏃 Object Tracking (`object_tracking.py`) 49 | - MeanShift 50 | - CamShift 51 | - KCF Tracker 52 | - CSRT Tracker 53 | 54 | ## 💡 Why Master Video Processing? 55 | 56 | Video processing with OpenCV is essential for computer vision, and here’s why it matters: 57 | 1. **Dynamic Analysis**: Enables motion detection, tracking, and stabilization for real-world applications. 58 | 2. **Versatility**: Applies to retail (e.g., customer tracking), autonomous systems, and surveillance. 59 | 3. **Interview Relevance**: Tested in coding challenges (e.g., background subtraction, object tracking). 60 | 4. **Performance**: Optimized for real-time video with OpenCV’s C++ backend. 61 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 62 | 63 | This section is your roadmap to mastering OpenCV’s video processing techniques for technical interviews—let’s dive in! 64 | 65 | ## 📆 Study Plan 66 | 67 | - **Week 1**: Video Basics 68 | - **Week 2**: Video Analysis 69 | - **Week 3**: Object Tracking 70 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 71 | 72 | ## 🛠️ Setup Instructions 73 | 74 | 1. **Python Environment**: 75 | - Install Python 3.8+ and pip. 76 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 77 | - Install dependencies: `pip install opencv-python numpy matplotlib`. 78 | 2. **Datasets**: 79 | - Uses synthetic video frames (e.g., moving shapes) generated with NumPy/OpenCV. 80 | - Optional: Use real video files or webcam input (commented out; enable locally). 81 | - Download sample videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 82 | 3. **Running Code**: 83 | - Run `.py` files in a Python environment (e.g., `python video_basics.py`). 84 | - Use Google Colab for convenience or local setup. 85 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 86 | - Check terminal for errors; ensure dependencies are installed. 87 | 4. **Webcam** (Optional): 88 | - `object_tracking.py` can use webcam input for real-time tracking (commented out; enable locally). 89 | 90 | ## 🏆 Practical Tasks 91 | 92 | 1. **Video Basics**: 93 | - Extract frames from synthetic video frames and save as images. 94 | - Print FPS and resolution of a synthetic video sequence. 95 | 2. **Video Analysis**: 96 | - Detect motion using frame differencing on synthetic frames. 97 | - Apply MOG2 background subtraction to isolate moving objects. 98 | 3. **Object Tracking**: 99 | - Track a moving square with MeanShift in synthetic frames. 100 | - Use CSRT tracker to follow a synthetic object. 101 | 102 | ## 💡 Interview Tips 103 | 104 | - **Common Questions**: 105 | - How does MOG2 background subtraction work? 106 | - What’s the difference between MeanShift and CSRT trackers? 107 | - How would you stabilize a shaky video? 108 | - **Tips**: 109 | - Explain background subtraction with code (e.g., `cv2.createBackgroundSubtractorMOG2()`). 110 | - Demonstrate tracking steps (e.g., initialize ROI → track with CSRT). 111 | - Be ready to code tasks like frame differencing or MeanShift tracking. 112 | - Discuss trade-offs between trackers (e.g., speed vs. accuracy). 113 | - **Coding Tasks**: 114 | - Implement frame differencing to detect motion. 115 | - Track an object using KCF tracker on synthetic frames. 116 | - Apply KNN background subtraction. 117 | - **Conceptual Clarity**: 118 | - Explain why MOG2 is robust for dynamic backgrounds. 119 | - Describe how CamShift adapts to object size changes. 120 | 121 | ## 📚 Resources 122 | 123 | - [OpenCV Official Documentation](https://docs.opencv.org/) 124 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 125 | - [PyImageSearch: Video Processing with OpenCV](https://www.pyimagesearch.com/category/opencv/) 126 | - [NumPy Documentation](https://numpy.org/doc/) 127 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 128 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 129 | 130 | ## 🤝 Contributions 131 | 132 | Love to collaborate? Here’s how! 🌟 133 | 1. Fork the repository. 134 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 135 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 136 | 4. Push to the branch (`git push origin feature/amazing-addition`). 137 | 5. Open a Pull Request. 138 | 139 | --- 140 | 141 |Happy Learning and Good Luck with Your Interviews! ✨
143 |Your guide to mastering advanced image processing with OpenCV for AI/ML and computer vision interviews
10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Advanced Image Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** section (e.g., `image_basics.py`), diving into sophisticated techniques like feature detection, image segmentation, and optical flow. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), and **Core Image Processing** (e.g., `image_filtering.py`)—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Feature Detection**: Detect corners (Harris, Shi-Tomasi), keypoints (ORB), match features (FLANN, Brute-Force), and estimate homography. 20 | - **Image Segmentation**: Perform contour detection, watershed algorithm, GrabCut, and K-Means clustering. 21 | - **Optical Flow**: Analyze motion with dense (Farneback) and sparse (Lucas-Kanade) optical flow, and track objects. 22 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic images and video frames. 23 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 24 | 25 | ## 🔍 Who Is This For? 26 | 27 | - Computer Vision Engineers advancing their skills. 28 | - Machine Learning Engineers exploring feature detection and segmentation. 29 | - AI Researchers mastering motion analysis. 30 | - Software Engineers deepening computer vision expertise. 31 | - Anyone preparing for advanced computer vision interviews in AI/ML or retail. 32 | 33 | ## 🗺️ Learning Roadmap 34 | 35 | This section covers three key areas, each with a dedicated `.py` file: 36 | 37 | ### 🕵️ Feature Detection (`feature_detection.py`) 38 | - Corner Detection (Harris, Shi-Tomasi) 39 | - Keypoint Detection (ORB) 40 | - Feature Matching (FLANN, Brute-Force) 41 | - Homography Estimation 42 | 43 | ### 🖲️ Image Segmentation (`image_segmentation.py`) 44 | - Contour Detection 45 | - Watershed Algorithm 46 | - GrabCut 47 | - Clustering (K-Means) 48 | 49 | ### 🛤️ Optical Flow (`optical_flow.py`) 50 | - Dense Optical Flow (Farneback) 51 | - Sparse Optical Flow (Lucas-Kanade) 52 | - Motion Tracking 53 | 54 | ## 💡 Why Master Advanced Image Processing? 55 | 56 | Advanced image processing with OpenCV is critical for computer vision, and here’s why it matters: 57 | 1. **Core Techniques**: Essential for object recognition, scene understanding, and motion analysis. 58 | 2. **Versatility**: Applies to retail (e.g., product recognition), autonomous systems, and augmented reality. 59 | 3. **Interview Relevance**: Tested in coding challenges (e.g., feature matching, segmentation). 60 | 4. **Performance**: Optimized for real-time applications with OpenCV’s C++ backend. 61 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 62 | 63 | This section is your roadmap to mastering OpenCV’s advanced techniques for technical interviews—let’s dive in! 64 | 65 | ## 📆 Study Plan 66 | 67 | - **Week 1**: Feature Detection 68 | - **Week 2**: Image Segmentation 69 | - **Week 3**: Optical Flow 70 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 71 | 72 | ## 🛠️ Setup Instructions 73 | 74 | 1. **Python Environment**: 75 | - Install Python 3.8+ and pip. 76 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 77 | - Install dependencies: `pip install opencv-python opencv-contrib-python numpy matplotlib`. 78 | 2. **Datasets**: 79 | - Uses synthetic images (e.g., shapes, patterns) and video frames generated with NumPy/OpenCV. 80 | - Optional: Download sample images/videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 81 | 3. **Running Code**: 82 | - Run `.py` files in a Python environment (e.g., `python feature_detection.py`). 83 | - Use Google Colab for convenience or local setup. 84 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 85 | - Check terminal for errors; ensure dependencies are installed. 86 | 4. **Webcam** (Optional): 87 | - `optical_flow.py` can use webcam input for real-time motion tracking (commented out; enable locally). 88 | 89 | ## 🏆 Practical Tasks 90 | 91 | 1. **Feature Detection**: 92 | - Detect Harris corners on a synthetic checkerboard pattern. 93 | - Match ORB keypoints between two synthetic images. 94 | 2. **Image Segmentation**: 95 | - Detect contours in a synthetic shape image. 96 | - Segment an object using GrabCut on a toy image. 97 | 3. **Optical Flow**: 98 | - Compute dense optical flow on synthetic video frames. 99 | - Track keypoints with Lucas-Kanade in a simulated motion sequence. 100 | 101 | ## 💡 Interview Tips 102 | 103 | - **Common Questions**: 104 | - What’s the difference between SIFT and ORB for keypoint detection? 105 | - How does the watershed algorithm work for segmentation? 106 | - What are the applications of optical flow in computer vision? 107 | - **Tips**: 108 | - Explain feature detection with code (e.g., `cv2.ORB_create()`). 109 | - Demonstrate segmentation steps (e.g., contour detection → watershed). 110 | - Be ready to code tasks like keypoint matching or motion tracking. 111 | - Discuss trade-offs between accuracy and speed (e.g., FLANN vs. Brute-Force). 112 | - **Coding Tasks**: 113 | - Implement Shi-Tomasi corner detection on a synthetic image. 114 | - Segment an object using K-Means clustering. 115 | - Compute sparse optical flow with Lucas-Kanade. 116 | - **Conceptual Clarity**: 117 | - Explain homography estimation for image alignment. 118 | - Describe how GrabCut uses graph cuts for segmentation. 119 | 120 | ## 📚 Resources 121 | 122 | - [OpenCV Official Documentation](https://docs.opencv.org/) 123 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 124 | - [PyImageSearch: Feature Detection and Segmentation](https://www.pyimagesearch.com/category/opencv/) 125 | - [NumPy Documentation](https://numpy.org/doc/) 126 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 127 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 128 | 129 | ## 🤝 Contributions 130 | 131 | Love to collaborate? Here’s how! 🌟 132 | 1. Fork the repository. 133 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 134 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 135 | 4. Push to the branch (`git push origin feature/amazing-addition`). 136 | 5. Open a Pull Request. 137 | 138 | --- 139 | 140 |Happy Learning and Good Luck with Your Interviews! ✨
142 |Your guide to mastering core image processing with OpenCV for AI/ML and computer vision interviews
10 | 11 | --- 12 | 13 | ## 📖 Introduction 14 | 15 | Welcome to the **Core Image Processing** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder is your starting point for mastering the foundational techniques of computer vision using OpenCV, covering image basics, transformations, filtering, and color processing. Designed for hands-on learning and interview success, it builds on your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), and **NumPy** (e.g., `array_creation_properties.py`)—and aligns with your retail-themed projects (April 26, 2025). Whether you’re preparing for coding challenges or technical discussions, this section equips you with the skills to shine in computer vision roles. 16 | 17 | ## 🌟 What’s Inside? 18 | 19 | - **Image Basics**: Read/write images, manipulate pixels, and explore color spaces (RGB, HSV, Grayscale). 20 | - **Image Transformations**: Resize, crop, rotate, flip, and apply affine/perspective transformations. 21 | - **Image Filtering**: Apply blurring, sharpening, edge detection, thresholding, and morphological operations. 22 | - **Color Processing**: Convert color spaces, filter colors, equalize histograms, and quantize colors. 23 | - **Hands-on Code**: Four `.py` files with practical examples using synthetic images. 24 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - Computer Vision Engineers building foundational skills. 29 | - Machine Learning Engineers exploring image processing. 30 | - AI Researchers mastering OpenCV basics. 31 | - Software Engineers transitioning to vision roles. 32 | - Anyone preparing for computer vision interviews in AI/ML or retail. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This section covers four key areas, each with a dedicated `.py` file: 37 | 38 | ### 📷 Image Basics (`image_basics.py`) 39 | - Reading/Writing Images 40 | - Color Spaces (RGB, HSV, Grayscale) 41 | - Pixel Manipulation 42 | - Image Properties (Shape, Size, Channels) 43 | 44 | ### 🖼️ Image Transformations (`image_transformations.py`) 45 | - Resizing 46 | - Cropping 47 | - Rotation 48 | - Flipping 49 | - Translation 50 | - Affine Transformations 51 | - Perspective Transformations 52 | 53 | ### 🎨 Image Filtering (`image_filtering.py`) 54 | - Blurring (Gaussian, Median, Bilateral) 55 | - Sharpening 56 | - Edge Detection (Sobel, Canny) 57 | - Thresholding (Binary, Adaptive) 58 | - Morphological Operations (Dilation, Erosion) 59 | 60 | ### 🌈 Color Processing (`color_processing.py`) 61 | - Color Space Conversion 62 | - Color Filtering 63 | - Histogram Equalization 64 | - Color Quantization 65 | 66 | ## 💡 Why Master Core Image Processing? 67 | 68 | Core image processing with OpenCV is the foundation of computer vision, and here’s why it matters: 69 | 1. **Fundamental Skills**: Essential for preprocessing images in AI/ML pipelines. 70 | 2. **Versatility**: Applies to retail, autonomous systems, medical imaging, and more. 71 | 3. **Interview Relevance**: Tested in coding challenges (e.g., edge detection, color filtering). 72 | 4. **Performance**: Optimized for real-time applications with OpenCV’s C++ backend. 73 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 74 | 75 | This section is your roadmap to mastering OpenCV’s core techniques for technical interviews—let’s dive in! 76 | 77 | ## 📆 Study Plan 78 | 79 | - **Week 1**: Image Basics and Transformations 80 | - **Week 2**: Image Filtering and Color Processing 81 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 82 | 83 | ## 🛠️ Setup Instructions 84 | 85 | 1. **Python Environment**: 86 | - Install Python 3.8+ and pip. 87 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 88 | - Install dependencies: `pip install opencv-python numpy matplotlib`. 89 | 2. **Datasets**: 90 | - Uses synthetic images generated with NumPy/OpenCV (e.g., rectangles, gradients). 91 | - Optional: Download sample images from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 92 | 3. **Running Code**: 93 | - Run `.py` files in a Python environment (e.g., `python image_basics.py`). 94 | - Use Google Colab for convenience or local setup. 95 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 96 | - Check terminal for errors; ensure dependencies are installed. 97 | 98 | ## 🏆 Practical Tasks 99 | 100 | 1. **Image Basics**: 101 | - Load a synthetic image and convert it to Grayscale and HSV. 102 | - Modify pixel values to create a red square. 103 | 2. **Image Transformations**: 104 | - Resize a synthetic image to half its size and rotate it by 45 degrees. 105 | - Apply an affine transformation to skew the image. 106 | 3. **Image Filtering**: 107 | - Apply Gaussian blur and Canny edge detection to a synthetic pattern. 108 | - Perform binary thresholding on a grayscale image. 109 | 4. **Color Processing**: 110 | - Filter the blue channel from a synthetic RGB image. 111 | - Apply histogram equalization to enhance contrast. 112 | 113 | ## 💡 Interview Tips 114 | 115 | - **Common Questions**: 116 | - What’s the difference between RGB and HSV color spaces? 117 | - How does Canny edge detection work, and what are its parameters? 118 | - Why use morphological operations in image processing? 119 | - **Tips**: 120 | - Explain color space conversions with code (e.g., `cv2.cvtColor(img, cv2.COLOR_BGR2HSV)`). 121 | - Demonstrate edge detection with clear steps (e.g., blur → Canny). 122 | - Be ready to code tasks like resizing or thresholding. 123 | - Discuss trade-offs between filter types (e.g., Gaussian vs. Bilateral). 124 | - **Coding Tasks**: 125 | - Convert an RGB image to Grayscale and display it. 126 | - Implement Sobel edge detection on a synthetic image. 127 | - Apply dilation to a binary image. 128 | - **Conceptual Clarity**: 129 | - Explain why HSV is useful for color filtering. 130 | - Describe how adaptive thresholding differs from binary thresholding. 131 | 132 | ## 📚 Resources 133 | 134 | - [OpenCV Official Documentation](https://docs.opencv.org/) 135 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 136 | - [PyImageSearch: Image Processing with OpenCV](https://www.pyimagesearch.com/category/opencv/) 137 | - [NumPy Documentation](https://numpy.org/doc/) 138 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 139 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 140 | 141 | ## 🤝 Contributions 142 | 143 | Love to collaborate? Here’s how! 🌟 144 | 1. Fork the repository. 145 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 146 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 147 | 4. Push to the branch (`git push origin feature/amazing-addition`). 148 | 5. Open a Pull Request. 149 | 150 | --- 151 | 152 |Happy Learning and Good Luck with Your Interviews! ✨
154 |Your guide to optimizing and deploying OpenCV applications for AI/ML and computer vision interviews
11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Optimization and Deployment** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`), **Advanced Image Processing** (e.g., `feature_detection.py`), **Video Processing** (e.g., `video_basics.py`), and **Object Detection and Recognition** (e.g., `deep_learning_detection.py`) sections, focusing on optimizing performance and deploying computer vision applications. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, **Advanced Image Processing**, **Video Processing**, and **Object Detection and Recognition**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Performance Optimization**: Optimize OpenCV with parallel processing, GPU acceleration, memory management, and algorithm efficiency. 21 | - **Deployment**: Deploy vision apps using Flask, Docker, and cloud platforms (AWS, GCP), with real-time inference. 22 | - **Integration**: Integrate OpenCV with TensorFlow, ROS, and embedded systems (Raspberry Pi). 23 | - **Hands-on Code**: Three `.py` files with practical examples using synthetic images and lightweight models. 24 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 25 | 26 | ## 🔍 Who Is This For? 27 | 28 | - Computer Vision Engineers optimizing and deploying vision systems. 29 | - Machine Learning Engineers integrating deep learning with OpenCV. 30 | - AI Researchers mastering real-time vision applications. 31 | - Software Engineers deploying vision solutions in production. 32 | - Anyone preparing for optimization and deployment interviews in AI/ML or retail. 33 | 34 | ## 🗺️ Learning Roadmap 35 | 36 | This section covers three key areas, each with a dedicated `.py` file: 37 | 38 | ### ⚡ Performance Optimization (`performance_optimization.py`) 39 | - Parallel Processing 40 | - GPU Acceleration (CUDA, Simulated) 41 | - Memory Management 42 | - Algorithm Efficiency 43 | 44 | ### 📦 Deployment (`deployment.py`) 45 | - Flask for Web Apps 46 | - Docker Containers (Conceptual) 47 | - Cloud Deployment (AWS, GCP, Conceptual) 48 | - Real-Time Inference 49 | 50 | ### 🔗 Integration (`integration.py`) 51 | - OpenCV with TensorFlow/PyTorch 52 | - OpenCV with ROS (Simulated) 53 | - OpenCV in Embedded Systems (Raspberry Pi, Conceptual) 54 | 55 | ## 💡 Why Master Optimization and Deployment? 56 | 57 | Optimization and deployment with OpenCV are critical for computer vision, and here’s why they matter: 58 | 1. **Performance**: Enables real-time vision applications with low latency. 59 | 2. **Scalability**: Supports production-ready deployment for retail (e.g., inventory tracking) and robotics. 60 | 3. **Interview Relevance**: Tested in system design and coding challenges (e.g., optimize OpenCV pipeline, deploy Flask app). 61 | 4. **Integration**: Combines OpenCV with modern ML frameworks and embedded systems. 62 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 63 | 64 | This section is your roadmap to mastering OpenCV optimization and deployment for technical interviews—let’s dive in! 65 | 66 | ## 📆 Study Plan 67 | 68 | - **Week 1**: Performance Optimization 69 | - **Week 2**: Deployment 70 | - **Week 3**: Integration 71 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 72 | 73 | ## 🛠️ Setup Instructions 74 | 75 | 1. **Python Environment**: 76 | - Install Python 3.8+ and pip. 77 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 78 | - Install dependencies: `pip install opencv-python numpy matplotlib tensorflow flask multiprocess`. 79 | 2. **Docker** (Optional for `deployment.py`): 80 | - Install Docker: [Docker Installation](https://docs.docker.com/get-docker/). 81 | - Use provided `Dockerfile` instructions in `deployment.py`. 82 | 3. **CUDA** (Optional for `performance_optimization.py`): 83 | - Requires NVIDIA GPU and OpenCV compiled with CUDA (not standard `opencv-python`). 84 | - Simulated in code; follow comments for local setup. 85 | 4. **ROS and Raspberry Pi** (Optional for `integration.py`): 86 | - Simulated; requires ROS Noetic and Raspberry Pi hardware for full setup. 87 | - Follow comments for conceptual steps. 88 | 5. **Datasets**: 89 | - Uses synthetic images (e.g., shapes) generated with NumPy/OpenCV. 90 | - MobileNetV2 uses pretrained weights from TensorFlow. 91 | 6. **Running Code**: 92 | - Run `.py` files in a Python environment (e.g., `python performance_optimization.py`). 93 | - For `deployment.py`, run Flask app locally (`flask run`) and access `http://127.0.0.1:5000`. 94 | - Use Google Colab for optimization and integration demos; local setup for Flask/Docker. 95 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 96 | - Check terminal for errors; ensure dependencies are installed. 97 | 98 | ## 🏆 Practical Tasks 99 | 100 | 1. **Performance Optimization**: 101 | - Parallelize image processing on synthetic images. 102 | - Optimize memory usage in an OpenCV pipeline. 103 | 2. **Deployment**: 104 | - Deploy a Flask app for real-time object detection. 105 | - Create a Docker container for a vision app (conceptual). 106 | 3. **Integration**: 107 | - Integrate OpenCV with TensorFlow for classification. 108 | - Simulate ROS node for vision processing. 109 | 110 | ## 💡 Interview Tips 111 | 112 | - **Common Questions**: 113 | - How do you optimize OpenCV for real-time performance? 114 | - What are the benefits of Docker for deploying vision apps? 115 | - How do you integrate OpenCV with TensorFlow for inference? 116 | - **Tips**: 117 | - Explain parallel processing with code (e.g., `multiprocess.Pool`). 118 | - Demonstrate Flask app setup (e.g., `Flask(__name__)`). 119 | - Be ready to code tasks like optimizing a detection pipeline or deploying a web app. 120 | - Discuss trade-offs (e.g., CPU vs. GPU, Flask vs. FastAPI). 121 | - **Coding Tasks**: 122 | - Parallelize face detection on multiple images. 123 | - Deploy a Flask app for image classification. 124 | - Integrate OpenCV with TensorFlow for object detection. 125 | - **Conceptual Clarity**: 126 | - Explain CUDA’s role in accelerating OpenCV. 127 | - Describe Docker’s containerization for vision apps. 128 | 129 | ## 📚 Resources 130 | 131 | - [OpenCV Official Documentation](https://docs.opencv.org/) 132 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 133 | - [PyImageSearch: OpenCV Optimization](https://www.pyimagesearch.com/category/opencv/) 134 | - [TensorFlow Documentation](https://www.tensorflow.org/api_docs) 135 | - [Flask Documentation](https://flask.palletsprojects.com/) 136 | - [Docker Documentation](https://docs.docker.com/) 137 | - [ROS Documentation](http://wiki.ros.org/) 138 | - [NumPy Documentation](https://numpy.org/doc/) 139 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 140 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 141 | 142 | ## 🤝 Contributions 143 | 144 | Love to collaborate? Here’s how! 🌟 145 | 1. Fork the repository. 146 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 147 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 148 | 4. Push to the branch (`git push origin feature/amazing-addition`). 149 | 5. Open a Pull Request. 150 | 151 | --- 152 | 153 |Happy Learning and Good Luck with Your Interviews! ✨
155 |Your guide to mastering object detection and recognition with OpenCV and deep learning for AI/ML interviews
11 | 12 | --- 13 | 14 | ## 📖 Introduction 15 | 16 | Welcome to the **Object Detection and Recognition** section of my Computer Vision with OpenCV (`cv2`) prep for AI/ML interviews! 🚀 This folder builds on the **Core Image Processing** (e.g., `image_basics.py`), **Advanced Image Processing** (e.g., `feature_detection.py`), and **Video Processing** (e.g., `video_basics.py`) sections, focusing on detecting and classifying objects in images. Designed for hands-on learning and interview success, it aligns with your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), **NumPy** (e.g., `array_creation_properties.py`), **Core Image Processing**, **Advanced Image Processing**, and **Video Processing**—and supports your retail-themed projects (April 26, 2025). Whether tackling coding challenges or technical discussions, this section equips you with the skills to excel in computer vision roles. 17 | 18 | ## 🌟 What’s Inside? 19 | 20 | - **Template Matching**: Detect single and multiple objects using template matching. 21 | - **Haar Cascades**: Perform face and eye detection with pretrained cascades. 22 | - **Deep Learning-Based Detection**: Use YOLOv3 for object detection. 23 | - **Image Classification**: Classify images with pretrained MobileNetV2 and transfer learning. 24 | - **Hands-on Code**: Four `.py` files with practical examples using synthetic images and CIFAR-10. 25 | - **Interview Scenarios**: Key questions and answers to ace computer vision interviews. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - Computer Vision Engineers advancing detection and recognition skills. 30 | - Machine Learning Engineers exploring deep learning-based detection. 31 | - AI Researchers mastering object classification. 32 | - Software Engineers deepening computer vision expertise. 33 | - Anyone preparing for object detection interviews in AI/ML or retail. 34 | 35 | ## 🗺️ Learning Roadmap 36 | 37 | This section covers four key areas, each with a dedicated `.py` file: 38 | 39 | ### 🔲 Template Matching (`template_matching.py`) 40 | - Single Object Matching 41 | - Multi-Object Matching 42 | 43 | ### 🧠 Haar Cascades (`haar_cascades.py`) 44 | - Face Detection 45 | - Eye Detection 46 | - Custom Cascade Training (Overview) 47 | 48 | ### 🤖 Deep Learning-Based Detection (`deep_learning_detection.py`) 49 | - YOLO (You Only Look Once) 50 | - SSD and Faster R-CNN (Discussed in Interview Scenario) 51 | - Model Integration (TensorFlow) 52 | 53 | ### 🏷️ Image Classification (`image_classification.py`) 54 | - Pretrained Models (MobileNetV2) 55 | - Transfer Learning 56 | - Custom Classification 57 | 58 | ## 💡 Why Master Object Detection and Recognition? 59 | 60 | Object detection and recognition with OpenCV and deep learning are critical for computer vision, and here’s why they matter: 61 | 1. **Core Techniques**: Essential for identifying and classifying objects in images. 62 | 2. **Versatility**: Applies to retail (e.g., product detection), autonomous systems, and surveillance. 63 | 3. **Interview Relevance**: Tested in coding challenges (e.g., face detection, YOLO implementation). 64 | 4. **Performance**: Combines OpenCV’s efficiency with deep learning’s accuracy. 65 | 5. **Industry Demand**: A must-have for 6 LPA+ computer vision roles. 66 | 67 | This section is your roadmap to mastering OpenCV and deep learning for object detection in technical interviews—let’s dive in! 68 | 69 | ## 📆 Study Plan 70 | 71 | - **Week 1**: Template Matching and Haar Cascades 72 | - **Week 2**: Deep Learning-Based Detection 73 | - **Week 3**: Image Classification 74 | - **Daily Practice**: Run one `.py` file, experiment with code, and review interview scenarios. 75 | 76 | ## 🛠️ Setup Instructions 77 | 78 | 1. **Python Environment**: 79 | - Install Python 3.8+ and pip. 80 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 81 | - Install dependencies: `pip install opencv-python numpy matplotlib tensorflow`. 82 | 2. **Datasets**: 83 | - Uses synthetic images (e.g., shapes, faces) generated with NumPy/OpenCV. 84 | - CIFAR-10 is used for classification (loaded via TensorFlow). 85 | - Optional: Download sample images from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 86 | 3. **YOLOv3 Weights**: 87 | - Download YOLOv3 weights and config: 88 | - `yolov3.weights`: [YOLOv3 Weights](https://pjreddie.com/media/files/yolov3.weights) 89 | - `yolov3.cfg`: [YOLOv3 Config](https://raw.githubusercontent.com/pjreddie/darknet/master/cfg/yolov3.cfg) 90 | - `coco.names`: [COCO Names](https://raw.githubusercontent.com/pjreddie/darknet/master/data/coco.names) 91 | - Place in the working directory for `deep_learning_detection.py`. 92 | 4. **Haar Cascades**: 93 | - Uses OpenCV’s pretrained XML files (included with `opencv-python`). 94 | 5. **Running Code**: 95 | - Run `.py` files in a Python environment (e.g., `python template_matching.py`). 96 | - Use Google Colab for convenience or local setup. 97 | - View outputs in terminal (console logs) and Matplotlib windows (saved as PNGs). 98 | - Check terminal for errors; ensure dependencies and YOLOv3 files are available. 99 | 100 | ## 🏆 Practical Tasks 101 | 102 | 1. **Template Matching**: 103 | - Detect a synthetic shape in an image using single template matching. 104 | - Find multiple instances of a template in a synthetic scene. 105 | 2. **Haar Cascades**: 106 | - Detect faces and eyes in a synthetic face image. 107 | - Explore custom cascade training steps (conceptual). 108 | 3. **Deep Learning-Based Detection**: 109 | - Run YOLOv3 on a synthetic image to detect objects. 110 | - Experiment with confidence thresholds in YOLOv3. 111 | 4. **Image Classification**: 112 | - Classify CIFAR-10 images using MobileNetV2. 113 | - Fine-tune MobileNetV2 for a custom retail dataset (simulated). 114 | 115 | ## 💡 Interview Tips 116 | 117 | - **Common Questions**: 118 | - How does template matching handle scale and rotation variations? 119 | - What are the limitations of Haar cascades for face detection? 120 | - Compare YOLO, SSD, and Faster R-CNN for object detection. 121 | - How does transfer learning improve image classification? 122 | - **Tips**: 123 | - Explain template matching with code (e.g., `cv2.matchTemplate()`). 124 | - Demonstrate YOLOv3 detection steps (e.g., load model → detect objects). 125 | - Be ready to code tasks like face detection or image classification. 126 | - Discuss trade-offs between detection methods (e.g., speed vs. accuracy). 127 | - **Coding Tasks**: 128 | - Implement multi-object template matching. 129 | - Detect faces using Haar cascades on a synthetic image. 130 | - Classify images with a pretrained MobileNetV2. 131 | - **Conceptual Clarity**: 132 | - Explain why YOLO is faster than Faster R-CNN. 133 | - Describe how Haar cascades use integral images for efficiency. 134 | 135 | ## 📚 Resources 136 | 137 | - [OpenCV Official Documentation](https://docs.opencv.org/) 138 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 139 | - [PyImageSearch: Object Detection](https://www.pyimagesearch.com/category/object-detection/) 140 | - [TensorFlow Documentation](https://www.tensorflow.org/api_docs) 141 | - [YOLO Official Site](https://pjreddie.com/darknet/yolo/) 142 | - [NumPy Documentation](https://numpy.org/doc/) 143 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 144 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 145 | 146 | ## 🤝 Contributions 147 | 148 | Love to collaborate? Here’s how! 🌟 149 | 1. Fork the repository. 150 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 151 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 152 | 4. Push to the branch (`git push origin feature/amazing-addition`). 153 | 5. Open a Pull Request. 154 | 155 | --- 156 | 157 |Happy Learning and Good Luck with Your Interviews! ✨
159 |Your comprehensive guide to mastering OpenCV (cv2) for AI/ML and computer vision interviews
12 | 13 | --- 14 | 15 | ## 📖 Introduction 16 | 17 | Welcome to my Computer Vision prep with OpenCV (`cv2`) for AI/ML interviews! 🚀 This repository is your essential guide for mastering OpenCV, the leading library for computer vision, with hands-on coding and interview-focused practice. From core image processing to advanced object detection and deep learning integration, it’s crafted to help you excel in technical interviews and computer vision projects with clarity and confidence. Building on your prior roadmaps—**Python** (e.g., `neural_networks.py`), **TensorFlow.js** (e.g., `ai_ml_javascript.js`), **GenAI** (e.g., `rag.py`), **JavaScript**, **Keras**, **Matplotlib** (e.g., `basic_plotting.py`), **Pandas** (e.g., `basic_operations.py`), and **NumPy** (e.g., `array_creation_properties.py`)—this section prepares you for roles in AI/ML, retail (aligned with April 26, 2025 projects), and computer vision. 18 | 19 | ## 🌟 What’s Inside? 20 | 21 | - **Core OpenCV Mastery**: Dive into image processing, filtering, and transformations. 22 | - **Advanced Techniques**: Explore feature detection, object tracking, and deep learning integration. 23 | - **Hands-on Practice**: Solve curated coding problems with detailed solutions. 24 | - **Interview Question Bank**: Tackle common computer vision questions with clear answers. 25 | - **Performance Optimization**: Learn tips for efficient, interview-ready OpenCV code. 26 | 27 | ## 🔍 Who Is This For? 28 | 29 | - Computer Vision Engineers prepping for technical interviews. 30 | - Machine Learning Engineers strengthening vision skills. 31 | - AI Researchers enhancing image processing expertise. 32 | - Software Engineers transitioning to computer vision roles. 33 | - Anyone mastering OpenCV for AI/ML and retail applications. 34 | 35 | ## 🗺️ Comprehensive Learning Roadmap 36 | 37 | --- 38 | 39 | ### 🖌️ Core Image Processing 40 | 41 | #### 📷 Image Basics 42 | - Reading/Writing Images 43 | - Color Spaces (RGB, HSV, Grayscale) 44 | - Pixel Manipulation 45 | - Image Properties (Shape, Size, Channels) 46 | 47 | #### 🖼️ Image Transformations 48 | - Resizing 49 | - Cropping 50 | - Rotation 51 | - Flipping 52 | - Translation 53 | - Affine Transformations 54 | - Perspective Transformations 55 | 56 | #### 🎨 Image Filtering 57 | - Blurring (Gaussian, Median, Bilateral) 58 | - Sharpening 59 | - Edge Detection (Sobel, Canny) 60 | - Thresholding (Binary, Adaptive) 61 | - Morphological Operations (Dilation, Erosion) 62 | 63 | #### 🌈 Color Processing 64 | - Color Space Conversion 65 | - Color Filtering 66 | - Histogram Equalization 67 | - Color Quantization 68 | 69 | --- 70 | 71 | ### 🔍 Advanced Image Processing 72 | 73 | #### 🕵️ Feature Detection 74 | - Corner Detection (Harris, Shi-Tomasi) 75 | - Keypoint Detection (SIFT, SURF, ORB) 76 | - Feature Matching (FLANN, Brute-Force) 77 | - Homography Estimation 78 | 79 | #### 🖲️ Image Segmentation 80 | - Contour Detection 81 | - Watershed Algorithm 82 | - GrabCut 83 | - Clustering (K-Means) 84 | 85 | #### 🛤️ Optical Flow 86 | - Dense Optical Flow (Farneback) 87 | - Sparse Optical Flow (Lucas-Kanade) 88 | - Motion Tracking 89 | 90 | --- 91 | 92 | ### 🎥 Video Processing 93 | 94 | #### 📹 Video Basics 95 | - Reading/Writing Videos 96 | - Frame Extraction 97 | - Video Properties (FPS, Resolution) 98 | 99 | #### 🕒 Video Analysis 100 | - Frame Differencing 101 | - Background Subtraction (MOG2, KNN) 102 | - Motion Detection 103 | - Video Stabilization 104 | 105 | #### 🏃 Object Tracking 106 | - MeanShift 107 | - CamShift 108 | - KCF Tracker 109 | - CSRT Tracker 110 | 111 | --- 112 | 113 | ### 🕵️♂️ Object Detection and Recognition 114 | 115 | #### 🔲 Template Matching 116 | - Single Object Matching 117 | - Multi-Object Matching 118 | 119 | #### 🧠 Haar Cascades 120 | - Face Detection 121 | - Eye Detection 122 | - Custom Cascade Training 123 | 124 | #### 🤖 Deep Learning-Based Detection 125 | - YOLO (You Only Look Once) 126 | - SSD (Single Shot MultiBox Detector) 127 | - Faster R-CNN 128 | - Model Integration (TensorFlow, PyTorch) 129 | 130 | #### 🏷️ Image Classification 131 | - Pretrained Models (ResNet, VGG, MobileNet) 132 | - Transfer Learning 133 | - Custom Classification 134 | 135 | --- 136 | 137 | ### 🛠️ Optimization and Deployment 138 | 139 | #### ⚡ Performance Optimization 140 | - Parallel Processing 141 | - GPU Acceleration (CUDA) 142 | - Memory Management 143 | - Algorithm Efficiency 144 | 145 | #### 📦 Deployment 146 | - Flask for Web Apps 147 | - Docker Containers 148 | - Cloud Deployment (AWS, GCP) 149 | - Real-Time Inference 150 | 151 | #### 🔗 Integration 152 | - OpenCV with TensorFlow/PyTorch 153 | - OpenCV with ROS (Robot Operating System) 154 | - OpenCV in Embedded Systems (Raspberry Pi) 155 | 156 | --- 157 | 158 | ## 💡 Why Master OpenCV for Computer Vision? 159 | 160 | OpenCV (`cv2`) is the cornerstone of computer vision, and here’s why: 161 | 1. **Versatility**: Supports the full vision pipeline—from preprocessing to deployment. 162 | 2. **Rich Functionality**: Packed with tools for image processing, detection, and tracking. 163 | 3. **Performance**: Optimized for real-time applications with C++ backend. 164 | 4. **Industry Demand**: A must-have skill for 6 LPA+ computer vision roles. 165 | 5. **Community Support**: Tap into a vast network of experts and resources. 166 | 167 | This repo is my roadmap to mastering OpenCV for technical interviews and computer vision careers—let’s build that skill set together! 168 | 169 | ## 📆 Study Plan 170 | 171 | - **Week 1-2**: Core Image Processing 172 | - **Week 3-4**: Advanced Image Processing and Feature Detection 173 | - **Week 5-6**: Video Processing and Object Tracking 174 | - **Week 7-8**: Object Detection and Deep Learning Integration 175 | - **Week 9-10**: Practical Applications and Deployment 176 | 177 | ## 🤝 Contributions 178 | 179 | Love to collaborate? Here’s how! 🌟 180 | 1. Fork the repository. 181 | 2. Create a feature branch (`git checkout -b feature/amazing-addition`). 182 | 3. Commit your changes (`git commit -m 'Add some amazing content'`). 183 | 4. Push to the branch (`git push origin feature/amazing-addition`). 184 | 5. Open a Pull Request. 185 | 186 | ## 📚 Resources 187 | 188 | - [OpenCV Official Documentation](https://docs.opencv.org/) 189 | - [OpenCV-Python Tutorials](https://opencv-python-tutroals.readthedocs.io/) 190 | - [“Learning OpenCV” by Gary Bradski and Adrian Kaehler](https://www.oreilly.com/library/view/learning-opencv/9780596516130/) 191 | - [“Deep Learning for Computer Vision with Python” by Adrian Rosebrock](https://www.pyimagesearch.com/) 192 | - [PyImageSearch Blog](https://www.pyimagesearch.com/) 193 | - [NumPy Documentation](https://numpy.org/doc/) 194 | - [Matplotlib Documentation](https://matplotlib.org/stable/contents.html) 195 | - [TensorFlow Documentation](https://www.tensorflow.org/) 196 | - [PyTorch Documentation](https://pytorch.org/docs/stable/index.html) 197 | 198 | ## 🛠️ Setup Instructions 199 | 200 | 1. **Python Environment**: 201 | - Install Python 3.8+ and pip. 202 | - Create a virtual environment: `python -m venv cv_env; source cv_env/bin/activate`. 203 | - Install dependencies: `pip install opencv-python opencv-contrib-python numpy matplotlib tensorflow torch`. 204 | 2. **Datasets**: 205 | - Use synthetic images (e.g., generated patterns) or public datasets (e.g., MNIST, COCO-like). 206 | - Download sample images/videos from [OpenCV Samples](https://github.com/opencv/opencv/tree/master/samples/data). 207 | 3. **Running Code**: 208 | - Run `.py` files in a Python environment (e.g., `python image_basics.py`). 209 | - Use Google Colab for GPU access if needed (e.g., deep learning models). 210 | - Check terminal for logs and errors; view images with Matplotlib or OpenCV windows. 211 | 4. **Web Deployment** (Optional): 212 | - Use Flask for web-based demos (e.g., `pip install flask`). 213 | - Deploy with Docker for production (e.g., `docker build -t cv-app .`). 214 | 5. **Hardware** (Optional): 215 | - Use a webcam for video processing demos. 216 | - Test on Raspberry Pi for embedded applications. 217 | 218 | ## 🏆 Practical Tasks 219 | 220 | 1. **Core Image Processing**: 221 | - Apply Gaussian blur and Canny edge detection to a synthetic image. 222 | - Convert an RGB image to HSV and filter a specific color. 223 | 2. **Advanced Image Processing**: 224 | - Detect keypoints with ORB and match features across two images. 225 | - Segment an object using GrabCut on a toy image. 226 | 3. **Video Processing**: 227 | - Extract frames from a sample video and detect motion. 228 | - Track an object with CSRT tracker in a webcam feed. 229 | 4. **Object Detection**: 230 | - Implement face detection with Haar cascades. 231 | - Use YOLO for real-time object detection on a toy dataset. 232 | 5. **Practical Applications**: 233 | - Build a retail product recognition system with template matching. 234 | - Detect lanes in a synthetic driving video. 235 | 6. **Optimization and Deployment**: 236 | - Optimize an edge detection pipeline for real-time performance. 237 | - Deploy a Flask app for webcam-based face detection. 238 | 239 | ## 💡 Interview Tips 240 | 241 | - **Common Questions**: 242 | - How does Canny edge detection work, and what are its parameters? 243 | - What’s the difference between Haar cascades and deep learning-based detection? 244 | - How would you optimize OpenCV for real-time video processing? 245 | - **Tips**: 246 | - Explain image processing pipelines with code snippets (e.g., Gaussian blur → Canny). 247 | - Demonstrate deep learning integration (e.g., YOLO with OpenCV). 248 | - Be ready to code tasks like contour detection or object tracking. 249 | - Discuss trade-offs between accuracy and speed in vision systems. 250 | - **Coding Tasks**: 251 | - Implement Sobel edge detection on a synthetic image. 252 | - Build a face detection system with Haar cascades. 253 | - Track an object in a video using KCF tracker. 254 | - **System Design**: 255 | - Design a real-time object detection pipeline with YOLO and Flask. 256 | - Explain GPU acceleration with OpenCV’s CUDA module. 257 | 258 | --- 259 | 260 |Happy Learning and Good Luck with Your Interviews! ✨
262 |