Skip to content

Commit

Permalink
Merge pull request #1099 from Sameeratweb/patch-1
Browse files Browse the repository at this point in the history
Create README.md
  • Loading branch information
UTSAVS26 authored Nov 7, 2024
2 parents 9e01ea7 + a362fcf commit 1c13688
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Computer Vision/Object Size Detection/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@

# Object Size Detection

This Project creates an app where users can upload an image to measure the size of objects in it. After uploading, the app detects objects, measures their height and width based on a reference size, and displays the image with dimensions marked on each object. It also shows the total number of objects found and lists each object’s size.


## Tech Stack

**Languages:** Python

**Libraries:** opencv,numpy etc

**Framework:** Streamlit

## Project Structure

Here's an overview of the main files and folders in this project:

```plaintext
├── app.py
├── requirements.txt
├── README.md
└── Results
├── res1.png
├── res2.png
└── res3.png
```
## Screenshots

![App Screenshot](Results/res1.png)
![App Screenshot](Results/res2.png)

## Run Locally

Install dependencies

```bash
pip install -r requirements.txt
```

Start the server

```bash
streamlit run app.py
```




Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 97 additions & 0 deletions Computer Vision/Object Size Detection/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import streamlit as st
from scipy.spatial import distance as dist
from imutils import perspective
from imutils import contours
import numpy as np
import imutils
import cv2

def midpoint(ptA, ptB):
return ((ptA[0] + ptB[0]) * 0.5, (ptA[1] + ptB[1]) * 0.5)

def process_image(image, width):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)

edged = cv2.Canny(gray, 50, 100)
edged = cv2.dilate(edged, None, iterations=1)
edged = cv2.erode(edged, None, iterations=1)

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
(cnts, _) = contours.sort_contours(cnts)

pixelsPerMetric = None
results = []

for c in cnts:
if cv2.contourArea(c) < 100:
continue

box = cv2.minAreaRect(c)
box = cv2.boxPoints(box)
box = np.array(box, dtype="int")
box = perspective.order_points(box)

(tl, tr, br, bl) = box
(tltrX, tltrY) = midpoint(tl, tr)
(blbrX, blbrY) = midpoint(bl, br)
(tlblX, tlblY) = midpoint(tl, bl)
(trbrX, trbrY) = midpoint(tr, br)

dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY))
dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY))

if pixelsPerMetric is None:
pixelsPerMetric = dB / width

dimA = dA / pixelsPerMetric
dimB = dB / pixelsPerMetric

results.append((box, dimA, dimB))

return results

def draw_results(image, results):
for (box, dimA, dimB) in results:
cv2.drawContours(image, [box.astype("int")], -1, (0, 255, 0), 2)

(tl, tr, br, bl) = box
(tltrX, tltrY) = midpoint(tl, tr)
(blbrX, blbrY) = midpoint(bl, br)
(tlblX, tlblY) = midpoint(tl, bl)
(trbrX, trbrY) = midpoint(tr, br)

cv2.circle(image, (int(tltrX), int(tltrY)), 5, (255, 0, 0), -1)
cv2.circle(image, (int(blbrX), int(blbrY)), 5, (255, 0, 0), -1)
cv2.circle(image, (int(tlblX), int(tlblY)), 5, (255, 0, 0), -1)
cv2.circle(image, (int(trbrX), int(trbrY)), 5, (255, 0, 0), -1)

cv2.line(image, (int(tltrX), int(tltrY)), (int(blbrX), int(blbrY)), (255, 0, 255), 2)
cv2.line(image, (int(tlblX), int(tlblY)), (int(trbrX), int(trbrY)), (255, 0, 255), 2)

cv2.putText(image, "{:.1f}in".format(dimA), (int(tltrX - 15), int(tltrY - 10)),
cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2)
cv2.putText(image, "{:.1f}in".format(dimB), (int(trbrX + 10), int(trbrY)),
cv2.FONT_HERSHEY_SIMPLEX, 0.65, (255, 255, 255), 2)


return image

st.title("Object Size Measurement")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
width = st.number_input("Width of the left-most object (in inches)", value=0.955, step=0.001)

if uploaded_file is not None:
image = cv2.imdecode(np.frombuffer(uploaded_file.read(), np.uint8), 1)
results = process_image(image, width)

output_image = draw_results(image.copy(), results)

st.image(output_image, channels="BGR", caption="Processed Image")

st.write(f"Number of objects detected: {len(results)}")

for i, (_, dimA, dimB) in enumerate(results, 1):
st.write(f"Object {i}: {dimA:.1f}in x {dimB:.1f}in")
8 changes: 8 additions & 0 deletions Computer Vision/Object Size Detection/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
imutils==0.5.4
numpy==2.1.2
opencv_contrib_python==4.10.0.84
opencv_python==4.10.0.82
opencv_python_headless==4.10.0.84
scipy==1.11.4
streamlit==1.30.0

2 changes: 2 additions & 0 deletions Project-Structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@
* [Project](Computer%20Vision/Ball-Tracking/Project.ipynb)
* Drowsiness Detection System
* [Main](Computer%20Vision/Drowsiness%20Detection%20System/main.py)
* Object Size Detection
* [App](Computer%20Vision/Object%20Size%20Detection/app.py)
* Volume Control Using Hand Gesture
* [Gesturevolume](Computer%20Vision/Volume%20Control%20Using%20Hand%20Gesture/GestureVolume.py)
* [Handtrackingmodule](Computer%20Vision/Volume%20Control%20Using%20Hand%20Gesture/HandTrackingModule.py)
Expand Down

0 comments on commit 1c13688

Please sign in to comment.