Skip to content

Commit af463db

Browse files
No public description
PiperOrigin-RevId: 815292692
1 parent 42081b9 commit af463db

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
# Dairy Product Detection Pipeline
2+
3+
This pipeline detects and extracts dairy product packets from a folder of image
4+
frames.
5+
6+
### Prerequisites
7+
8+
- GCP account with Compute Engine access
9+
- A folder containing image frames to process
10+
11+
### Setup Instructions
12+
13+
### 1. Create a VM Instance
14+
15+
### 2. Download the Setup Script
16+
17+
SSH into your VM instance and run:
18+
19+
```bash
20+
curl -o setup.sh https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/setup.sh
21+
```
22+
23+
### 3. Run the Setup Script
24+
25+
Execute the setup script to download all required files and dependencies:
26+
27+
```bash
28+
bash setup.sh
29+
```
30+
31+
This will automatically download all necessary files for running the
32+
detection pipeline.
33+
34+
### 4. Process Your Images
35+
36+
Given a folder path containing your test images, run:
37+
38+
```bash
39+
bash run_pipeline.sh --input_dir=/path/to/test_images
40+
```
41+
42+
Replace `/path/to/test_images` with the actual path to your image folder.
43+
44+
### 5. View Results
45+
46+
The pipeline will create two folders inside your input directory:
47+
48+
- **`dairy/`** - Contains all cropped objects identified as dairy products
49+
- **`others/`** - Contains all cropped objects that are not dairy products
50+
51+
### Example
52+
53+
```bash
54+
# If your images are in /home/user/test_images
55+
bash run_pipeline.sh --input_dir=/home/user/test_images
56+
57+
# Results will be in:
58+
# /home/user/test_images/dairy/
59+
# /home/user/test_images/others/
60+
```
61+
62+
### Troubleshooting
63+
64+
- Ensure your VM has sufficient memory and disk space
65+
- Verify that all image files are in supported formats (JPG, PNG, etc.)
66+
- Check that you have proper read/write permissions for the input directory
67+
68+
## Dataset Creation for Training ML Models
69+
70+
This guide explains how to create datasets for training image classifier, object
71+
detection, or instance segmentation models from images of a particular
72+
category.
73+
74+
### 1. Prepare Your Images
75+
76+
Organize your images into a folder. These should be images containing objects of
77+
a particular category (e.g., dairy products, bottles, cans, etc.).
78+
79+
### 2. Run the Extract Objects Script
80+
81+
Execute the following command to extract objects and generate dataset files:
82+
83+
```python
84+
python3 extract_objects.py --input_dir=/test_images --category_name=dairy
85+
```
86+
87+
Replace:
88+
89+
- `/test_images` with the path to your image folder.
90+
- `dairy` with your category name (e.g., bottles, cans, plastic, etc.)
91+
92+
### 3. Generated Outputs
93+
94+
The script will generate two types of outputs inside your input directory:
95+
96+
#### For Image Classification Models
97+
98+
A folder named **`tempdir/`** will be created containing:
99+
100+
- All cropped objects extracted from the images
101+
- These cropped images can be directly used to train an image classifier model
102+
103+
#### For Object Detection/Segmentation Models
104+
105+
A COCO format JSON file will be generated containing:
106+
107+
- Annotations for all detected objects
108+
- Bounding boxes and segmentation masks
109+
- This file can be used to train object detection or instance segmentation models
110+
111+
### Example Usage
112+
113+
```python
114+
# Extract dairy products from images
115+
python3 extract_objects.py --input_dir=/home/user/dairy_images --category_name=dairy
116+
117+
# Extract plastic bottles
118+
python3 extract_objects.py --input_dir=/home/user/bottle_images --category_name=bottles
119+
120+
# Extract metal cans
121+
python3 extract_objects.py --input_dir=/home/user/can_images --category_name=cans
122+
```
123+
124+
### Output Structure
125+
126+
After running the script, your directory will look like:
127+
128+
```
129+
/test_images/
130+
├── image1.jpg
131+
├── image2.jpg
132+
├── tempdir/ # Cropped objects for classification
133+
│ ├── crop_001.jpg
134+
│ ├── crop_002.jpg
135+
│ └── ...
136+
└── annotations.json # COCO format file for detection/segmentation
137+
```
138+
139+
### Use Cases
140+
141+
- **Image Classification**: Use images from `tempdir/` folder
142+
- **Object Detection**: Use the COCO JSON file with original images
143+
- **Instance Segmentation**: Use the COCO JSON file with segmentation masks
144+
145+
### Tips
146+
147+
- Ensure your images are clear and objects are visible
148+
- Use consistent naming for category names across your datasets
149+
- Verify the generated annotations before training your models

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/classify_images.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import os
1919
import shutil
2020

21+
from absl import app
2122
from absl import flags
2223
import torch
2324
import tqdm
@@ -61,3 +62,6 @@ def main(_) -> None:
6162
shutil.move(path, os.path.join(dairy_folder, os.path.basename(path)))
6263
else:
6364
shutil.move(path, os.path.join(other_folder, os.path.basename(path)))
65+
66+
if __name__ == "__main__":
67+
app.run(main)

official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/setup.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ wget -q -P ./milk_pouch_project/image_classifier_model https://storage.googleapi
8484
echo "✅ Finished: Download Image Classifier Model"
8585
echo "-----"
8686

87+
echo "Download the required files locally and modify the imports."
88+
curl -sS -o models.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/models.py
89+
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection import models_utils|import models_utils|g' models.py
90+
91+
curl -sS -o classify_images.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/classify_images.py
92+
sed -i 's|from official.projects.waste_identification_ml.llm_applications.milk_pouch_detection import models|import models|g' classify_images.py
93+
94+
curl -sS -o models_utils.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/models_utils.py
95+
curl -sS -o extract_objects.py https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/extract_objects.py
96+
curl -sS -o run_pipeline.sh https://raw.githubusercontent.com/tensorflow/models/master/official/projects/waste_identification_ml/llm_applications/milk_pouch_detection/run_pipeline.sh
97+
echo "Files downloaded and modified successfully!"
98+
8799
# --- Completion ---
88100
echo "🎉🎉🎉 Environment setup complete! 🎉🎉🎉"
89101
echo "-----"

0 commit comments

Comments
 (0)