Code With Python
39K subscribers
841 photos
24 videos
22 files
746 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 Exception Handling — Managing Errors Gracefully

---

Why Handle Exceptions?

• To prevent your program from crashing unexpectedly.

• To provide meaningful error messages or recovery actions.

---

Basic Try-Except Block

try:
result = 10 / 0
except ZeroDivisionError:
print("Cannot divide by zero!")


---

Catching Multiple Exceptions

try:
x = int(input("Enter a number: "))
result = 10 / x
except (ValueError, ZeroDivisionError) as e:
print(f"Error occurred: {e}")


---

Using Else and Finally

else block runs if no exceptions occur.

finally block always runs, used for cleanup.

try:
file = open("data.txt", "r")
data = file.read()
except FileNotFoundError:
print("File not found.")
else:
print("File read successfully.")
finally:
file.close()


---

Raising Exceptions

• You can raise exceptions manually using raise.

def check_age(age):
if age < 0:
raise ValueError("Age cannot be negative.")

check_age(-1)


---

Custom Exceptions

• Create your own exception classes by inheriting from Exception.

class MyError(Exception):
pass

def do_something():
raise MyError("Something went wrong!")

try:
do_something()
except MyError as e:
print(e)


---

Summary

• Use try-except to catch and handle errors.

• Use else and finally for additional control.

• Raise exceptions to signal errors.

• Define custom exceptions for specific needs.

---

#Python #ExceptionHandling #Errors #Debugging #ProgrammingTips
2
Topic: Python List vs Tuple — Differences and Use Cases

---

Key Differences

Lists are mutable — you can change, add, or remove elements.

Tuples are immutable — once created, they cannot be changed.

---

Creating Lists and Tuples

my_list = [1, 2, 3]
my_tuple = (1, 2, 3)


---

When to Use Each

• Use lists when you need a collection that can change over time.

• Use tuples when the collection should remain constant, providing safer and faster data handling.

---

Common Tuple Uses

• Returning multiple values from a function.

def get_coordinates():
return (10, 20)

x, y = get_coordinates()


• Using as keys in dictionaries (since tuples are hashable, lists are not).

---

Converting Between Lists and Tuples

list_to_tuple = tuple(my_list)
tuple_to_list = list(my_tuple)


---

Performance Considerations

• Tuples are slightly faster than lists due to immutability.

---

Summary

Lists: mutable, dynamic collections.

Tuples: immutable, fixed collections.

• Choose based on whether data should change or stay constant.

---

#Python #Lists #Tuples #DataStructures #ProgrammingTips

https://t.me/DataScience4
1
Topic: Mastering Recursion — From Basics to Advanced Applications

---

What is Recursion?

• Recursion is a technique where a function calls itself to solve smaller instances of a problem until reaching a base case.

---

Basic Structure

• Every recursive function needs:

* A base case to stop recursion.

* A recursive case that breaks the problem into smaller parts.

---

Simple Example: Fibonacci Numbers

def fibonacci(n):
if n <= 1:
return n # base case
else:
return fibonacci(n-1) + fibonacci(n-2) # recursive case


---

Drawbacks of Naive Recursion

• Repeated calculations cause exponential time complexity.

• Can cause stack overflow on large inputs.

---

Improving Recursion: Memoization

• Store results of subproblems to avoid repeated work.

memo = {}
def fib_memo(n):
if n in memo:
return memo[n]
if n <= 1:
memo[n] = n
else:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]


---

Advanced Concepts

Tail Recursion: Recursive call is the last operation. Python does not optimize tail calls but understanding it is important.

Divide and Conquer Algorithms: Recursion breaks problems into subproblems (e.g., Merge Sort, Quick Sort).

---

Example: Merge Sort

def merge_sort(arr):
if len(arr) <= 1:
return arr

mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])

return merge(left, right)

def merge(left, right):
result = []
i = j = 0

while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1

result.extend(left[i:])
result.extend(right[j:])
return result


---

Exercise

• Implement a recursive function to solve the Tower of Hanoi problem for *n* disks and print the moves.

---

#Algorithms #Recursion #Memoization #DivideAndConquer #CodingExercise

https://t.me/DataScience4
4
Topic: Python File Handling — Reading, Writing, and Managing Files (Beginner to Advanced)

---

What is File Handling?

• File handling allows Python programs to read from and write to external files — such as .txt, .csv, .json, etc.

• Python uses built-in functions like open(), read(), and write() to interact with files.

---

Opening a File

file = open("example.txt", "r")  # "r" = read mode
content = file.read()
file.close()


---

Using with Statement (Best Practice)

• Automatically handles file closing:

with open("example.txt", "r") as file:
content = file.read()


---

File Modes

"r" — read (default)
"w" — write (creates or overwrites)
"a" — append (adds to the end)
"x" — create (fails if file exists)
"b" — binary mode
"t" — text mode (default)

---

Writing to Files

with open("output.txt", "w") as file:
file.write("Hello, world!")


Note: "w" overwrites existing content.

---

Appending to Files

with open("output.txt", "a") as file:
file.write("\nNew line added.")


---

Reading Line by Line

with open("example.txt", "r") as file:
for line in file:
print(line.strip())


---

Working with File Paths

• Use os.path or pathlib for platform-independent paths.

from pathlib import Path

file_path = Path("folder") / "file.txt"
with open(file_path, "r") as f:
print(f.read())


---

Advanced Tip: Reading and Writing CSV Files

import csv

with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["name", "age"])
writer.writerow(["Alice", 30])


with open("data.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)


---

Summary

• Use open() with correct mode to read/write files.

• Prefer with statement to manage files safely.

• Use libraries like csv, json, or pickle for structured data.

• Always handle exceptions like FileNotFoundError for robust file operations.

---

Exercise

• Write a Python program that reads a list of names from names.txt, sorts them alphabetically, and saves the result in sorted_names.txt.

---

#Python #FileHandling #ReadWrite #DataProcessing #ProgrammingTips

https://t.me/DataScience4
3
Topic: Django Models and ORM — From Basics to Advanced Queries

---

What is a Model in Django?

• A model in Django is a Python class that defines the structure of your database table. Each model maps to a table, and each attribute represents a column.

• Django uses its ORM (Object-Relational Mapping) to interact with the database using Python code instead of SQL.

---

Creating Your First Model

from django.db import models

class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
published_date = models.DateField()
pages = models.IntegerField()


---

Making Migrations

• Create and apply migrations to sync models with the database:

python manage.py makemigrations
python manage.py migrate


---

Using the Model

# Creating a new record
book = Book(title="1984", author="George Orwell", published_date="1949-06-08", pages=328)
book.save()

# Fetching all books
books = Book.objects.all()

# Filtering
orwell_books = Book.objects.filter(author="George Orwell")

# Getting one object
book = Book.objects.get(id=1)

# Updating
book.title = "Animal Farm"
book.save()

# Deleting
book.delete()


---

Model Field Types

CharField, TextField, IntegerField, FloatField, DateField, DateTimeField, BooleanField, EmailField, and more.

---

Meta Class for Model Options

class Book(models.Model):
title = models.CharField(max_length=200)

class Meta:
ordering = ['title'] # default ordering by title


---

Relationships Between Models

One-to-Many (ForeignKey)
Many-to-Many (ManyToManyField)
One-to-One (OneToOneField)

class Author(models.Model):
name = models.CharField(max_length=100)

class Book(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(Author, on_delete=models.CASCADE)


---

Advanced ORM Queries

# Complex filters
books = Book.objects.filter(published_date__year__gte=2000, pages__lte=300)

# Exclude
books = Book.objects.exclude(author="J.K. Rowling")

# Ordering
books = Book.objects.order_by("-published_date")

# Count
total = Book.objects.count()


---

Summary

• Django models define your database structure.

• The ORM allows you to query and manipulate data using Python.

• Supports relationships, complex filtering, ordering, and aggregation.

---

Exercise

• Create two models: Author and Book. Link them using a foreign key. Then, write views that:

1. Add a new book.
2. List all books by a specific author.
3. Delete books published before the year 2000.

---

#Django #WebDevelopment #ORM #DatabaseModels #DjangoTips

https://t.me/DataScience4
4
Topic: Django ORM – Advanced Queries, Aggregations, and Query Optimization (Part 2)

---

1. Aggregation Functions

• Django provides built-in functions for aggregating data.

from django.db.models import Avg, Sum, Max, Min, Count

# Average number of pages
avg_pages = Book.objects.aggregate(Avg("pages"))

# Total number of pages
total_pages = Book.objects.aggregate(Sum("pages"))

# Count of books per author
book_counts = Book.objects.values("author").annotate(total=Count("id"))


---

2. Grouping and Annotating

annotate() is used to compute values for each row (e.g., totals per group).

# Number of books per author
from django.db.models import Count

authors = Author.objects.annotate(book_count=Count("book"))
for author in authors:
print(author.name, author.book_count)


---

3. Complex Lookups with Q Objects

• Use Q for OR, AND, and NOT conditions.

from django.db.models import Q

# Books with title containing 'war' OR author name 'Leo Tolstoy'
books = Book.objects.filter(Q(title__icontains="war") | Q(author__name="Leo Tolstoy"))

# Books not published in 2023
books = Book.objects.filter(~Q(published_date__year=2023))


---

4. Selecting Specific Fields

• Use values() or values\_list() to retrieve specific fields.

# Dictionary of titles and authors
data = Book.objects.values("title", "author__name")

# List of titles
titles = Book.objects.values_list("title", flat=True)


---

5. Related Model Queries

• Use select\_related and prefetch\_related to optimize related data access.

# Optimized: Single JOIN query for ForeignKey
books = Book.objects.select_related("author")

# For ManyToMany or reverse relations
authors = Author.objects.prefetch_related("book_set")


---

6. Raw SQL Queries (When Necessary)

books = Book.objects.raw("SELECT * FROM myapp_book WHERE pages > %s", [300])
for book in books:
print(book.title)


---

7. Performance Tips

• Use only() or defer() to limit retrieved fields.

books = Book.objects.only("title")


• Avoid chaining queries in loops.

• Use bulk\_create, bulk\_update for inserting/updating many records.

---

Summary

• Use aggregate(), annotate(), and Q objects for powerful filtering.

• Fetch only what you need using values, only, and select\_related.

• Optimize queries by reducing database hits and using Django’s ORM efficiently.

---

Exercise

• Write a Django query that returns all authors with more than 5 books, sorted by the number of books (descending). Then print their name and book count.

---

#Django #ORM #AdvancedQueries #QueryOptimization #WebDevelopment

https://t.me/DataScience4
2👍2
Topic: Django ORM – Transactions, Subqueries, and Custom Managers (Part 3)

---

1. Working with Transactions

• Django supports atomic transactions to ensure database integrity — either all operations succeed, or none do.

from django.db import transaction

@transaction.atomic
def create_author_and_book():
author = Author.objects.create(name="New Author")
Book.objects.create(title="New Book", author=author)


• Use atomic() as a decorator or context manager.

with transaction.atomic():
# multiple operations that must succeed together
...


---

2. Subqueries and OuterRef

• Use Subquery and OuterRef to perform queries that depend on other queries.

from django.db.models import Subquery, OuterRef

# Get latest book for each author
latest_books = Book.objects.filter(author=OuterRef('pk')).order_by('-published_date')
authors = Author.objects.annotate(latest_book=Subquery(latest_books.values('title')[:1]))


---

3. Exists() and Conditional Logic

• Use Exists for optimized existence checks.

from django.db.models import Exists

recent_books = Book.objects.filter(published_date__year=2023)
authors = Author.objects.annotate(has_recent_books=Exists(recent_books.filter(author=OuterRef('pk'))))


---

4. Custom Model Managers

• Add custom query logic to models via custom managers.

from django.db import models

class PublishedBookManager(models.Manager):
def get_queryset(self):
return super().get_queryset().filter(is_published=True)

class Book(models.Model):
title = models.CharField(max_length=200)
is_published = models.BooleanField(default=False)

objects = models.Manager() # Default manager
published = PublishedBookManager() # Custom manager


# Usage
Book.published.all()


---

5. QuerySet Methods: Update, Delete, Bulk Operations

update() modifies multiple records efficiently.

Book.objects.filter(author__name="Alice").update(pages=300)


delete() removes objects in bulk.

Book.objects.filter(published_date__year__lt=2000).delete()


bulk\_create() inserts many records at once.

Book.objects.bulk_create([
Book(title="Book A", author=author),
Book(title="Book B", author=author),
])


---

6. Using Database Functions

• Django provides built-in SQL functions like Lower, Upper, Length, Concat, etc.

from django.db.models.functions import Upper

books = Book.objects.annotate(upper_title=Upper('title'))


---

Summary

• Use transactions to maintain data integrity.

• Leverage subqueries, OuterRef, and Exists for complex logic.

• Create custom managers to encapsulate reusable query logic.

• Apply bulk operations and DB functions for performance and flexibility.

---

Exercise

• Create a custom manager for the Book model to return only books published in the last 5 years. Then use this manager in a view to list all recent books along with their authors.

---

#Django #ORM #Transactions #Subqueries #CustomManagers #AdvancedDjango

https://t.me/DataScience4
5
Topic: Linked Lists in Python – Part 1: Introduction and Singly Linked List Basics

---

What is a Linked List?

• A linked list is a linear data structure where each element (called a node) contains:

• The data value.
• A pointer (or reference) to the next node.

• Unlike arrays, linked lists don’t require contiguous memory and can grow dynamically.

---

Why Use Linked Lists?

Efficient insertions/deletions at the beginning or middle.
No need for pre-defining size, unlike arrays.
• Used in memory-efficient applications like OS kernels, compilers, and real-time systems.

---

Types of Linked Lists

Singly Linked List – Each node points to the next.
Doubly Linked List – Nodes have next and previous pointers.
Circular Linked List – The last node points back to the head.

---

Basic Structure of a Node

class Node:
def __init__(self, data):
self.data = data
self.next = None


---

Building a Singly Linked List

class LinkedList:
def __init__(self):
self.head = None

def append(self, data):
new_node = Node(data)

if not self.head:
self.head = new_node
return

current = self.head
while current.next:
current = current.next
current.next = new_node


---

Traversing the List

def display(self):
current = self.head
while current:
print(current.data, end=" -> ")
current = current.next
print("None")


Usage:

ll = LinkedList()
ll.append(10)
ll.append(20)
ll.append(30)
ll.display() # Output: 10 -> 20 -> 30 -> None


---

Inserting at the Beginning

def insert_at_beginning(self, data):
new_node = Node(data)
new_node.next = self.head
self.head = new_node


---

Summary

• A singly linked list stores data as a sequence of nodes linked by references.

• Supports dynamic memory usage, fast insertions, and flexible resizing.

• The key is managing node connections safely and efficiently.

---

Exercise

• Implement a method length() that returns the number of nodes in the list.

---

#DataStructures #LinkedList #DSA #Python #CodingBasics

https://t.me/DataScience4
3
Topic: Linked Lists in Python – Part 2: Insertion, Deletion, and Search Operations

---

Recap from Part 1

• A singly linked list consists of nodes, where each node holds data and a pointer to the next node.

• We've implemented basic append and display functions.

Now we’ll explore insertion at specific positions, deletion, and searching.

---

1. Insert at a Specific Position

def insert_at_position(self, index, data):
if index < 0:
raise IndexError("Index cannot be negative")

new_node = Node(data)

if index == 0:
new_node.next = self.head
self.head = new_node
return

current = self.head
for _ in range(index - 1):
if not current:
raise IndexError("Index out of bounds")
current = current.next

new_node.next = current.next
current.next = new_node


---

2. Delete a Node by Value

def delete_by_value(self, value):
if not self.head:
return

if self.head.data == value:
self.head = self.head.next
return

current = self.head
while current.next and current.next.data != value:
current = current.next

if current.next:
current.next = current.next.next


---

3. Delete a Node by Index

def delete_by_index(self, index):
if index < 0:
raise IndexError("Index cannot be negative")

if not self.head:
raise IndexError("List is empty")

if index == 0:
self.head = self.head.next
return

current = self.head
for _ in range(index - 1):
if not current.next:
raise IndexError("Index out of bounds")
current = current.next

if current.next:
current.next = current.next.next


---

4. Search for an Element

def search(self, value):
current = self.head
index = 0
while current:
if current.data == value:
return index
current = current.next
index += 1
return -1 # Not found


---

5. Complete Class with All Methods

class LinkedList:
def __init__(self):
self.head = None

def append(self, data): ...
def display(self): ...
def insert_at_position(self, index, data): ...
def delete_by_value(self, value): ...
def delete_by_index(self, index): ...
def search(self, value): ...


*(You can reuse the method definitions above.)*

---

Summary

• You can manipulate linked lists with insertions and deletions at any position.

• Searching through a singly linked list is O(n).

• Always check for edge cases: empty list, index bounds, and duplicates.

---

Exercise

• Write a method reverse() that reverses the linked list in-place and test it on a list of 5+ elements.

---

#DSA #LinkedList #Python #Insertion #Deletion #Search

https://t.me/DataScience4
2
Topic: Linked Lists in Python – Part 3: Reversing, Detecting Loops, and Middle Node

---

In this part, we’ll explore advanced operations that are frequently asked in coding interviews using singly linked lists.

---

1. Reverse a Linked List (Iterative)

def reverse(self):
prev = None
current = self.head
while current:
next_node = current.next
current.next = prev
prev = current
current = next_node
self.head = prev


Time Complexity: O(n)
Space Complexity: O(1)

---

2. Find the Middle of the Linked List

• Use the slow and fast pointer approach.

def find_middle(self):
slow = fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow.data if slow else None


---

3. Detect a Loop in the Linked List

• Use Floyd’s Cycle Detection Algorithm (a.k.a. Tortoise and Hare).

def has_loop(self):
slow = fast = self.head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False


---

4. Find the N-th Node from the End

def nth_from_end(self, n):
first = self.head
second = self.head

for _ in range(n):
if not first:
return None
first = first.next

while first:
first = first.next
second = second.next

return second.data


---

5. Full Example: Testing All Methods

ll = LinkedList()
for value in [10, 20, 30, 40, 50]:
ll.append(value)

ll.display()
ll.reverse()
ll.display()
print("Middle:", ll.find_middle())
print("Has Loop?", ll.has_loop())
print("3rd from End:", ll.nth_from_end(3))


---

Summary

• Use pointer manipulation for efficient algorithms in linked lists.

• Common techniques:
Fast/slow pointers
Reversal by in-place re-linking
Two-pointer gap approach for nth from end

---

Exercise

• Write a method is_palindrome() that checks if the linked list reads the same forwards and backwards (without converting it to a list or using extra space).

---

#DSA #LinkedList #ReverseList #LoopDetection #TwoPointerTechnique

https://t.me/DataScience4
5
Topic: Linked Lists in Python – Part 4: Merging, Sorting, and Advanced Interview Problems

---

In this final part of the Linked List series, we’ll tackle more advanced and practical use cases like merging sorted lists, sorting a linked list, and interview-level challenges.

---

1. Merging Two Sorted Linked Lists

def merge_sorted_lists(l1, l2):
dummy = Node(0)
tail = dummy

while l1 and l2:
if l1.data < l2.data:
tail.next = l1
l1 = l1.next
else:
tail.next = l2
l2 = l2.next
tail = tail.next

tail.next = l1 or l2
return dummy.next


---

2. Sorting a Linked List (Merge Sort – O(n log n))

def merge_sort(head):
if not head or not head.next:
return head

# Split list
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next

mid = slow.next
slow.next = None

left = merge_sort(head)
right = merge_sort(mid)

return merge_sorted_lists(left, right)


• Attach this to your LinkedList class to sort self.head.

---

3. Removing Duplicates from a Sorted List

def remove_duplicates(self):
current = self.head
while current and current.next:
if current.data == current.next.data:
current.next = current.next.next
else:
current = current.next


---

4. Intersection Point of Two Linked Lists

def get_intersection_node(headA, headB):
if not headA or not headB:
return None

a, b = headA, headB
while a != b:
a = a.next if a else headB
b = b.next if b else headA
return a # Can be None or the intersecting node


---

5. Flatten a Multilevel Linked List

Imagine a list where each node has next and child pointers. Recursively flatten:

def flatten(node):
if not node:
return None

next_node = node.next
if node.child:
child_tail = flatten(node.child)
node.next = node.child
node.child = None
child_tail.next = flatten(next_node)
return child_tail if child_tail.next else child_tail
else:
node.next = flatten(next_node)
return node


---

Summary

• You now know how to merge, sort, and deduplicate linked lists.

• Techniques like merge sort, two-pointer traversal, and recursive flattening are essential for mastering linked lists.

• These problems are frequently asked in interviews at top tech companies.

---

Exercise

• Given two unsorted linked lists, write a function that returns a new linked list containing only the elements present in both lists (intersection), without using extra space or sets.

---

#DSA #LinkedList #MergeSort #AdvancedDSA #CodingInterview

https://t.me/DataScience4
2
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.me/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.me/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.me/DataScience4
3
🚀 THE 7-DAY PROFIT CHALLENGE! 🚀

Can you turn $100 into $5,000 in just 7 days?
Jay can. And she’s challenging YOU to do the same. 👇

https://t.me/+QOcycXvRiYs4YTk1
https://t.me/+QOcycXvRiYs4YTk1
https://t.me/+QOcycXvRiYs4YTk1
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.me/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.me/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.me/DataScience4
4
Topic: Python – Reading Images from Datasets and Organizing Them (Part 2): Using PyTorch and TensorFlow Data Loaders

---

1. Using PyTorch’s `ImageFolder` and `DataLoader`

PyTorch provides an easy way to load image datasets organized in folders by classes.

from torchvision import datasets, transforms
from torch.utils.data import DataLoader

# Define transformations (resize, normalize, convert to tensor)
transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])

dataset = datasets.ImageFolder(root='dataset/', transform=transform)

# Create DataLoader for batching and shuffling
dataloader = DataLoader(dataset, batch_size=32, shuffle=True)

# Access class names
class_names = dataset.classes
print(class_names)


---

2. Iterating Through DataLoader

for images, labels in dataloader:
print(images.shape) # (batch_size, 3, 128, 128)
print(labels)
# Use images and labels for training or validation
break


---

3. Using TensorFlow `image_dataset_from_directory`

TensorFlow Keras also provides utilities for loading datasets organized in folders.

import tensorflow as tf

dataset = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
image_size=(128, 128),
batch_size=32,
label_mode='int' # can be 'categorical', 'binary', or None
)

class_names = dataset.class_names
print(class_names)

for images, labels in dataset.take(1):
print(images.shape)
print(labels)


---

4. Dataset Splitting

You can split datasets into training and validation sets easily:

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
validation_split=0.2,
subset="training",
seed=123,
image_size=(128, 128),
batch_size=32
)

val_ds = tf.keras.preprocessing.image_dataset_from_directory(
'dataset/',
validation_split=0.2,
subset="validation",
seed=123,
image_size=(128, 128),
batch_size=32
)


---

5. Summary

• PyTorch’s ImageFolder + DataLoader offers a quick way to load and batch datasets.

• TensorFlow’s image\_dataset\_from\_directory provides similar high-level dataset loading.

• Both allow easy transformations, batching, and shuffling.

---

Exercise

• Write code to normalize images in TensorFlow dataset using map() with Rescaling.

---

#Python #DatasetHandling #PyTorch #TensorFlow #ImageProcessing

https://t.me/DataScience4
4
Topic: Python – Reading Images from Datasets and Organizing Them (Part 3): Custom Dataset Class and Data Augmentation

---

1. Creating a Custom Dataset Class (PyTorch)

Sometimes you need more control over how images and labels are loaded and processed. You can create a custom dataset class by extending torch.utils.data.Dataset.

import os
from PIL import Image
from torch.utils.data import Dataset

class CustomImageDataset(Dataset):
def __init__(self, root_dir, transform=None):
self.root_dir = root_dir
self.transform = transform
self.image_paths = []
self.labels = []
self.class_to_idx = {}

classes = sorted(os.listdir(root_dir))
self.class_to_idx = {cls_name: idx for idx, cls_name in enumerate(classes)}

for cls_name in classes:
cls_dir = os.path.join(root_dir, cls_name)
for img_name in os.listdir(cls_dir):
img_path = os.path.join(cls_dir, img_name)
self.image_paths.append(img_path)
self.labels.append(self.class_to_idx[cls_name])

def __len__(self):
return len(self.image_paths)

def __getitem__(self, idx):
img_path = self.image_paths[idx]
image = Image.open(img_path).convert("RGB")
label = self.labels[idx]

if self.transform:
image = self.transform(image)

return image, label


---

2. Using Data Augmentation with `transforms`

Data augmentation helps improve model generalization by artificially increasing dataset diversity.

from torchvision import transforms

transform = transforms.Compose([
transforms.Resize((128, 128)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(10),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])


Pass this transform to the custom dataset:

dataset = CustomImageDataset(root_dir='dataset/', transform=transform)


---

3. Loading Dataset with DataLoader

from torch.utils.data import DataLoader

dataloader = DataLoader(dataset, batch_size=32, shuffle=True)


---

4. Summary

• Custom dataset classes offer flexibility in how data is loaded and labeled.

• Data augmentation techniques such as flipping and rotation can be applied using torchvision transforms.

• Use DataLoader for batching and shuffling during training.

---

Exercise

• Extend the custom dataset to handle grayscale images and apply a random brightness adjustment transform.

---

#Python #DatasetHandling #PyTorch #DataAugmentation #ImageProcessing

https://t.me/DataScience4
2
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.me/DataScience4
3