Code With Python
39.2K subscribers
886 photos
27 videos
22 files
769 links
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Download Telegram
Topic: Python OpenCV – Part 1: Introduction, Image Reading, and Basic Operations

---

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is a powerful computer vision and image processing library.

• It supports image and video capture, analysis, object detection, face recognition, and much more.

• Commonly used with Python, C++, and machine learning pipelines.

---

Installing OpenCV

pip install opencv-python


---

1. Reading and Displaying Images

import cv2

# Read the image
image = cv2.imread('image.jpg')

# Display the image in a window
cv2.imshow('My Image', image)
cv2.waitKey(0) # Wait for any key to close the window
cv2.destroyAllWindows()


---

2. Image Shape and Type

print(image.shape)  # (height, width, channels)
print(image.dtype) # uint8 (8-bit integers for each channel)


---

3. Converting Color Spaces

# Convert BGR to Grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Convert BGR to RGB (for matplotlib or image correction)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


---

4. Saving an Image

cv2.imwrite('gray_image.png', gray)


---

5. Drawing Shapes

# Draw a red rectangle
cv2.rectangle(image, (50, 50), (200, 200), (0, 0, 255), 2)

# Draw a filled circle
cv2.circle(image, (150, 150), 40, (255, 0, 0), -1)

# Draw text
cv2.putText(image, 'OpenCV!', (40, 300), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)

cv2.imshow('Drawn Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()


---

6. Resize and Flip

# Resize image
resized = cv2.resize(image, (300, 300))

# Flip image horizontally
flipped = cv2.flip(image, 1)


---

Summary

OpenCV allows you to read, display, modify, and save images easily.

• You can perform basic tasks like drawing, resizing, flipping, and color transformations.

• These operations are the building blocks for image analysis, preprocessing, and machine vision applications.

---

Exercise

• Write a program that:

1. Loads an image.
2. Converts it to grayscale.
3. Draws a blue circle in the center.
4. Saves the new image to disk.

---

#Python #OpenCV #ImageProcessing #ComputerVision #Beginners

https://t.iss.one/DataScience4
2
Topic: Python OpenCV – Part 2: Image Thresholding, Blurring, and Edge Detection

---

1. Image Thresholding

• Converts grayscale images to binary (black & white) by setting a pixel value threshold.

import cv2

image = cv2.imread('image.jpg', 0) # load as grayscale

_, binary = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)

cv2.imshow("Binary Image", binary)
cv2.waitKey(0)
cv2.destroyAllWindows()


• Common thresholding types:

* THRESH_BINARY
* THRESH_BINARY_INV
* THRESH_TRUNC
* THRESH_TOZERO
* THRESH_OTSU (automatic threshold)

_, otsu = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)


---

2. Image Blurring (Smoothing)

• Helps reduce image noise and detail.

# Gaussian Blur
blurred = cv2.GaussianBlur(image, (5, 5), 0)

# Median Blur
median = cv2.medianBlur(image, 5)

# Bilateral Filter (preserves edges)
bilateral = cv2.bilateralFilter(image, 9, 75, 75)


---

3. Edge Detection with Canny

• Canny edge detection is one of the most popular techniques for detecting edges in an image.

edges = cv2.Canny(image, 100, 200)

cv2.imshow("Edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()


* The two thresholds (100 and 200) are used for hysteresis (strong vs. weak edges).

---

4. Combining Blur + Edge Detection

blurred = cv2.GaussianBlur(image, (5, 5), 0)
edges = cv2.Canny(blurred, 50, 150)


• Blurring before edge detection reduces noise and improves accuracy.

---

Summary

Thresholding simplifies images to black & white based on intensity.

Blurring smooths out images and reduces noise.

Canny Edge Detection is a reliable method for identifying edges.

• These tools are fundamental for object detection, image segmentation, and OCR tasks.

---

Exercise

• Write a program that:

1. Loads an image in grayscale.
2. Applies Gaussian blur.
3. Uses Canny to detect edges.
4. Saves the final edge-detected image to disk.

---

#Python #OpenCV #ImageProcessing #EdgeDetection #ComputerVision

https://t.iss.one/DataScience4
2
Topic: Python OpenCV – Part 3: Contours, Morphological Operations, and Image Masking

---

1. Finding Contours

• Contours are curves joining all continuous points along a boundary having the same color or intensity.

import cv2

image = cv2.imread('shapes.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Threshold to get binary image
_, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# Find contours
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

# Draw contours
cv2.drawContours(image, contours, -1, (0, 255, 0), 3)

cv2.imshow("Contours", image)
cv2.waitKey(0)
cv2.destroyAllWindows()


---

2. Morphological Operations

• Used to remove noise or fill gaps.

* Erosion: Removes pixels on object boundaries.

* Dilation: Adds pixels to object boundaries.

kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))

eroded = cv2.erode(thresh, kernel, iterations=1)
dilated = cv2.dilate(thresh, kernel, iterations=1)


• Other useful operations:

* Opening: erosion followed by dilation (removes noise).

* Closing: dilation followed by erosion (closes small holes).

opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)
closing = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)


---

3. Image Masking

• Use a mask to isolate part of an image.

mask = cv2.cvtColor(thresh, cv2.COLOR_GRAY2BGR)
masked_img = cv2.bitwise_and(image, mask)

cv2.imshow("Masked Image", masked_img)
cv2.waitKey(0)
cv2.destroyAllWindows()


---

Summary

Contours help detect shapes and boundaries.

Morphological operations clean up binary images by removing noise or filling holes.

Masks isolate regions of interest for further processing.

---

Exercise

• Load an image with multiple shapes, use contour detection to count the number of distinct shapes, then draw bounding rectangles around each.

---

#Python #OpenCV #Contours #Morphology #ImageMasking

https://t.iss.one/DataScience4
3
Topic: Python OpenCV – Part 4: Video Processing, Webcam Capture, and Real-Time Operations

---

1. Reading Video from File

import cv2

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

while cap.isOpened():
ret, frame = cap.read()
if not ret:
break

cv2.imshow('Video Frame', frame)

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

cap.release()
cv2.destroyAllWindows()


---

2. Capturing Video from Webcam

cap = cv2.VideoCapture(0)  # 0 is usually the built-in webcam

while True:
ret, frame = cap.read()
if not ret:
break

cv2.imshow('Webcam Feed', frame)

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

cap.release()
cv2.destroyAllWindows()


---

3. Saving Video to File

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
break

out.write(frame) # write frame to output file
cv2.imshow('Recording', frame)

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

cap.release()
out.release()
cv2.destroyAllWindows()


---

4. Real-Time Video Processing

• Example: Convert webcam feed to grayscale live.

cap = cv2.VideoCapture(0)

while True:
ret, frame = cap.read()
if not ret:
break

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayscale Webcam', gray)

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

cap.release()
cv2.destroyAllWindows()


---

Summary

• You can capture video from files or webcams easily with OpenCV.

• Real-time processing lets you modify frames on the fly (filters, detections).

• Saving video requires specifying codec, frame rate, and resolution.

---

Exercise

• Write a program that captures webcam video, applies Canny edge detection to each frame in real-time, and saves the processed video to disk. Quit when pressing 'q'.

---

#Python #OpenCV #VideoProcessing #Webcam #RealTime

https://t.iss.one/DataScience4
3
Topic: 20 Important Python OpenCV Interview Questions with Brief Answers

---

1. What is OpenCV?
OpenCV is an open-source library for computer vision, image processing, and machine learning.

2. How do you read and display an image using OpenCV?
Use cv2.imread() to read and cv2.imshow() to display images.

3. What color format does OpenCV use by default?
OpenCV uses BGR format instead of RGB.

4. How to convert an image from BGR to Grayscale?
Use cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).

5. What is the difference between `cv2.imshow()` and matplotlib’s `imshow()`?
cv2.imshow() uses BGR and OpenCV windows; matplotlib uses RGB and inline plotting.

6. How do you write/save an image to disk?
Use cv2.imwrite('filename.jpg', image).

7. What are image thresholds?
Techniques to segment images into binary images based on pixel intensity.

8. What is Gaussian Blur, and why is it used?
A smoothing filter to reduce image noise and detail.

9. Explain the Canny Edge Detection process.
It detects edges using gradients, non-maximum suppression, and hysteresis thresholding.

10. How do you capture video from a webcam using OpenCV?
Use cv2.VideoCapture(0) and read frames in a loop.

11. What are contours in OpenCV?
Curves joining continuous points with the same intensity, used for shape detection.

12. How do you find and draw contours?
Use cv2.findContours() and cv2.drawContours().

13. What are morphological operations?
Operations like erosion and dilation that process shapes in binary images.

14. What is the difference between erosion and dilation?
Erosion removes pixels on object edges; dilation adds pixels.

15. How to perform image masking in OpenCV?
Use cv2.bitwise_and() with an image and a mask.

16. What is the use of `cv2.waitKey()`?
Waits for a key event for a specified time; needed to display images properly.

17. How do you resize an image?
Use cv2.resize(image, (width, height)).

18. Explain how to save a video using OpenCV.
Use cv2.VideoWriter() with codec, fps, and frame size.

19. How to convert an image from OpenCV BGR format to RGB for matplotlib?
Use cv2.cvtColor(image, cv2.COLOR_BGR2RGB).

20. What is the difference between `cv2.threshold()` and `cv2.adaptiveThreshold()`?
cv2.threshold() applies global threshold; adaptiveThreshold() applies varying thresholds locally.

---

Summary

These questions cover basic to intermediate OpenCV concepts essential for interviews and practical use.

---

#Python #OpenCV #InterviewQuestions #ComputerVision #ImageProcessing

https://t.iss.one/DataScience4
3
Topic: Python – Reading Images from Datasets and Organizing Them

---

1. Reading Images from Folder Structure

Assuming your dataset folder looks like this:

dataset/
class1/
img1.jpg
img2.jpg
class2/
img3.jpg
img4.jpg


You can use Python libraries like os, OpenCV, or PIL to read and organize images by their classes.

---

2. Code Example Using OpenCV

import os
import cv2

dataset_path = "dataset"
data = []
labels = []

for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
data.append(img)
labels.append(class_name)

print(f"Total images: {len(data)}")
print(f"Total labels: {len(labels)}")


---

3. Optional: Resize Images for Uniformity

target_size = (128, 128)
resized_img = cv2.resize(img, target_size)


Use this inside the loop before appending img to data.

---

4. Using PIL (Pillow) Instead of OpenCV

from PIL import Image

img = Image.open(img_path)
img = img.resize((128, 128))
img_array = np.array(img)


---

5. Organizing Images in a Dictionary

dataset_dict = {}

for class_name in os.listdir(dataset_path):
class_dir = os.path.join(dataset_path, class_name)
if os.path.isdir(class_dir):
dataset_dict[class_name] = []
for img_name in os.listdir(class_dir):
img_path = os.path.join(class_dir, img_name)
img = cv2.imread(img_path)
if img is not None:
dataset_dict[class_name].append(img)


---

6. Summary

• Use os.listdir() to iterate dataset directories.

• Read images with cv2.imread() or PIL.Image.open().

• Resize images to a uniform shape for model input.

• Store images and labels in lists or dictionaries for easy access.

---

Exercise

• Extend the code to save the loaded images and labels as numpy arrays for faster loading in the future.

---

#Python #ImageProcessing #DatasetHandling #OpenCV #PIL

https://t.iss.one/DataScience4
4
Topic: 20 Important Python Questions on Reading and Organizing Images from Datasets

---

1. How can you read images from a directory using Python?
Use libraries like OpenCV (cv2.imread) or PIL (Image.open).

2. How do you organize images by class labels if they are stored in subfolders?
Iterate over each subfolder, treat folder names as labels, and map images accordingly.

3. What is the difference between OpenCV and PIL for image reading?
OpenCV reads images in BGR format and uses NumPy arrays; PIL uses RGB and has more image manipulation utilities.

4. How do you resize images before feeding them to a model?
Use cv2.resize() or PIL’s resize() method.

5. What is a good practice to handle different image sizes in datasets?
Resize all images to a fixed size or use data loaders that apply transformations.

6. How to convert images to NumPy arrays?
In OpenCV, images are already NumPy arrays; with PIL, use np.array(image).

7. How do you normalize images?
Scale pixel values, typically to \[0,1] by dividing by 255 or standardize with mean and std.

8. How can you load large datasets efficiently?
Use generators or data loaders to load images batch-wise instead of loading all at once.

9. What is `torchvision.datasets.ImageFolder`?
A PyTorch utility to load images from a directory with subfolders as class labels.

10. How do you apply transformations and augmentations during image loading?
Use torchvision.transforms or TensorFlow preprocessing layers.

11. How can you split datasets into training and validation sets?
Use libraries like sklearn.model_selection.train_test_split or parameters in dataset loaders.

12. How do you handle corrupted or unreadable images during loading?
Use try-except blocks to catch exceptions and skip those files.

13. How do you batch images for training deep learning models?
Use DataLoader in PyTorch or TensorFlow datasets with batching enabled.

14. What are common image augmentations used during training?
Flips, rotations, scaling, cropping, color jittering, and normalization.

15. How do you convert labels (class names) to numeric indices?
Create a mapping dictionary from class names to indices.

16. How can you visualize images and labels after loading?
Use matplotlib’s imshow() and print labels alongside.

17. How to read images in grayscale?
With OpenCV: cv2.imread(path, cv2.IMREAD_GRAYSCALE).

18. How to save processed images after loading?
Use cv2.imwrite() or PIL.Image.save().

19. How do you organize dataset information (images and labels) in Python?
Use lists, dictionaries, or pandas DataFrames.

20. How to handle imbalanced datasets?
Use class weighting, oversampling, or undersampling techniques during data loading.

---

Summary

Mastering image loading and organization is fundamental for effective data preprocessing in computer vision projects.

---

#Python #ImageProcessing #DatasetHandling #OpenCV #DeepLearning

https://t.iss.one/DataScience4
3
🚀 Comprehensive Tutorial: Build a Folder Monitoring & Intruder Detection System in Python

In this comprehensive, step-by-step tutorial, you will learn how to build a real-time folder monitoring and intruder detection system using Python.

🔐 Your Goal:
- Create a background program that:
- Monitors a specific folder on your computer.
- Instantly captures a photo using the webcam whenever someone opens that folder.
- Saves the photo with a timestamp in a secure folder.
- Runs automatically when Windows starts.
- Keeps running until you manually stop it (e.g., via Task Manager or a hotkey).

Read and get code: https://hackmd.io/@husseinsheikho/Build-a-Folder-Monitoring

#Python #Security #FolderMonitoring #IntruderDetection #OpenCV #FaceCapture #Automation #Windows #TaskScheduler #ComputerVision

✉️ Our Telegram channels: https://t.iss.one/addlist/0f6vfFbEMdAwODBk

📱 Our WhatsApp channel: https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Please open Telegram to view this post
VIEW IN TELEGRAM
4👍1🔥1
#YOLOv8 #ComputerVision #FireDetection #Python #AI #Safety

Lesson: Real-Time Fire Detection in an Industrial Facility with YOLOv8 and Alarm System

This tutorial guides you through building a computer vision project from scratch. We will use the YOLOv8 model to detect fire in a video feed from an industrial setting and trigger an alarm sound upon detection.

---

#Step 1: Project Setup and Dependencies

First, we need to install the necessary Python libraries. We'll use ultralytics for the YOLOv8 model, opencv-python for video processing, and playsound to trigger our alarm. Open your terminal or command prompt and run the following command:

pip install ultralytics opencv-python playsound

After installation, create a Python file (e.g., fire_detector.py) and import these libraries.

import cv2
from ultralytics import YOLO
from playsound import playsound
import threading

# Hashtags: #Setup #Python #OpenCV #YOLOv8


---

#Step 2: Load the Model and Prepare the Alarm System

We will load a pre-trained YOLOv8 model. For a real-world application, you must train a custom model on a dataset of fire and smoke images. For this example, we will write the code assuming you have a custom model named fire_model.pt that knows how to detect 'fire'.

You also need an alarm sound file (e.g., alarm.wav) in the same directory as your script.

# Load your custom-trained YOLOv8 model
# IMPORTANT: The standard YOLOv8 models do not detect 'fire'.
# You must train your own model on a fire dataset.
model = YOLO('fire_model.pt') # Replace with your custom model path

# Path to your alarm sound file
ALARM_SOUND_PATH = "alarm.wav"

# A flag to ensure the alarm plays only once per detection event
alarm_on = False

def play_alarm():
"""Plays the alarm sound in a separate thread."""
global alarm_on
print("ALARM: Fire Detected!")
playsound(ALARM_SOUND_PATH)
alarm_on = False # Reset alarm flag after sound finishes

# Hashtags: #AIModel #AlarmSystem #SafetyFirst


---

#Step 3: Main Loop for Video Processing and Detection

This is the core of our application. We will open a video file, read it frame by frame, and pass each frame to our YOLOv8 model for inference. If the model detects 'fire' with a certain confidence, we will draw a bounding box around it and trigger the alarm.

Create a video file named industrial_video.mp4 or use your own video source.
1
# Open the video file
video_path = 'industrial_video.mp4'
cap = cv2.VideoCapture(video_path)

# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()

if success:
# Run YOLOv8 inference on the frame
results = model(frame)

# A flag to check if fire was detected in the current frame
fire_detected_in_frame = False

# Visualize the results on the frame
annotated_frame = results[0].plot()

# Process detection results
for r in results:
for box in r.boxes:
# Check if the detected class is 'fire'
# model.names[0] should correspond to 'fire' in your custom model
if model.names[int(box.cls[0])] == 'fire' and box.conf[0] > 0.5:
fire_detected_in_frame = True
break

# If fire is detected and alarm is not already on, trigger alarm
if fire_detected_in_frame and not alarm_on:
alarm_on = True
# Run the alarm sound in a background thread to not block video feed
alarm_thread = threading.Thread(target=play_alarm)
alarm_thread.start()

# Display the annotated frame
cv2.imshow("YOLOv8 Fire Detection", annotated_frame)

# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()

# Hashtags: #RealTimeDetection #VideoProcessing #OpenCV


---

#Step 4: Results and Discussion

After running the script, you will see a window playing the video. When the model detects an object it identifies as 'fire' with a confidence score above 50%, it will:
• Draw a colored box around the fire.
• Print "ALARM: Fire Detected!" to the console.
• Play the alarm.wav sound.

Discussion of Results:
Model Performance: The accuracy of this system depends entirely on the quality of your custom-trained model (fire_model.pt). A model trained on a diverse dataset of industrial fires (different lighting, angles, sizes) will perform best.
False Positives: The system might incorrectly identify orange/red lights, reflections, or welding sparks as fire. This is a common challenge. To fix this, you need to add more "negative" images (images of things that look like fire but aren't) to your training dataset.
Thresholding: The confidence threshold (box.conf[0] > 0.5) is a critical parameter. A lower value increases the chance of detecting real fires but also increases false alarms. A higher value reduces false alarms but might miss smaller or less obvious fires. You must tune this value based on your specific environment.
Real-World Implementation: For a real industrial facility, you would replace the video file with a live camera stream (cv2.VideoCapture(0) for a webcam) and integrate the alarm logic with a physical siren or a central monitoring system via an API or GPIO pins.

#ProjectComplete #AIforGood #IndustrialSafety

━━━━━━━━━━━━━━━
By: @DataScience4
1