Spaces:
Running on Zero
Running on Zero
Enrich demo with examples and documentation
Browse files123456789101112131415161718192021MIT License
Copyright (c) 2026 Mingze Chen
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THESOFTWARE.11 unmodified lines12131415161718192021222324252627282930313233343536373839404142434445464748495051525311 unmodified linesshort_description: Street image segmentation with SegFormer.---# Street Scene SegmentationA small Hugging Face Space for uploading street-view imagery and generating:- a semantic-segmentation overlay;- a Cityscapes color mask;- per-class pixel coverage;- downloadable overlay, mask, raw class-ID PNG, and CSV files.The app uses[`nvidia/segformer-b0-finetuned-cityscapes-1024-1024`](https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024).It is intentionally small enough to run on a free CPU Space, while automaticallyusing CUDA when a GPU is available. The inference function is also decorated forHugging Face ZeroGPU, allowing eligible free personal accounts to host the appwithout creating a paid CPU Space.## DeployCreate a new **Gradio Space**. If your account offers **ZeroGPU**, select it inthe hardware picker. Then push these files to the Space repository:```bashgit remote add space https://huggingface.co/spaces/<USERNAME>/<SPACE_NAME>git push space main```No API key or Space secret is required because the model is public.## APIAfter deployment, the Gradio endpoint is exposed as `segment`. The Space's**Use via API** panel provides a generated Python and JavaScript example.## Notes- The model predicts the 19 Cityscapes street-scene classes.- Images larger than 2048 pixels on their longest side are resized before output.- Model-license and dataset terms remain governed by the linked model card andCityscapes terms.11 unmodified lines1213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121211 unmodified linesshort_description: Street image segmentation with SegFormer.---<div align="center"># 🚦 Street Scene Semantic Segmentation### Pixel-level urban scene understanding with SegFormer and Cityscapes[](https://huggingface.co/spaces/Mingze/StreetSceneSegmentation)[](https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024)[](https://www.python.org/)[](https://www.gradio.app/)[](LICENSE)**Upload a street image and turn every pixel into an interpretable map of the urban environment.**[**🚀 Launch the live app**](https://huggingface.co/spaces/Mingze/StreetSceneSegmentation) ·[**🧠 View the model**](https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024) ·[**💻 Source code**](https://github.com/LabMingzeChen/StreetSceneSegmentation)</div>---## Live demoThe public [Hugging Face Space](https://huggingface.co/spaces/Mingze/StreetSceneSegmentation)runs entirely in the browser interface. Students can upload an image, choose abuilt-in example, inspect the results, and download the outputs without installingPython or providing an API key.| Sample street image | Example segmentation result ||---|---||  |  |## What the project doesThe application uses[`nvidia/segformer-b0-finetuned-cityscapes-1024-1024`](https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024)to perform semantic segmentation. Unlike object detection, which draws one boxaround an object, semantic segmentation assigns a class to **every image pixel**.For each image, the app produces:| Output | Purpose ||---|---|| Segmentation overlay | Keeps the original scene visible beneath the predicted classes || Cityscapes color mask | Shows a clean, standardized categorical map || Class coverage table | Reports class ID, pixel count, area share, and color || Raw class-ID PNG | Preserves the numeric prediction for later analysis || CSV summary | Supports statistics, plotting, and classroom exercises |## The 19 Cityscapes classes| Group | Classes | Example questions ||---|---|---|| Travel surfaces | `road`, `sidewalk` | How much of the view supports vehicle or pedestrian movement? || Built environment | `building`, `wall`, `fence`, `pole`, `traffic light`, `traffic sign` | Which fixed urban elements dominate the scene? || Nature and sky | `vegetation`, `terrain`, `sky` | How visually green or open is the street? || People | `person`, `rider` | Where are active street users visible? || Transport | `car`, `truck`, `bus`, `train`, `motorcycle`, `bicycle` | Which transport modes appear in the image? |The reported area share is the percentage of **image pixels** assigned to aclass. It is a measure of visual coverage, not physical land area.## How it works```textStreet image↓ resize and normalizeSegFormer-B0 image processor↓ transformer-based semantic segmentation19-class pixel map├── original-image overlay├── Cityscapes color mask├── raw class-ID image└── per-class CSV summary```The model is downloaded from Hugging Face on the first request and then reusedfrom the container cache. CUDA is selected when available; local execution alsoworks on CPU. Images larger than 2048 pixels on their longest side are resizedto limit memory use while preserving aspect ratio.## Classroom and research examples- **Urban greenery:** compare the visible share of vegetation across streets.- **Street design:** examine how roads, sidewalks, buildings, and terrain divide the view.- **Mobility:** compare the presence of cars, bicycles, buses, riders, and pedestrians.- **Model literacy:** inspect boundary errors and discuss why predictions differ from human interpretation.- **Change over time:** process repeat photographs of a location and compare their CSV summaries.Four sample street scenes are loaded from the[HNIVision example collection](https://github.com/LabMingzeChen/HNIVision/tree/main/space/examples)and appear directly beneath the upload panel in the live app. Hosting samplesthrough GitHub Raw follows the same lightweight pattern as HNI-VLM and avoidsduplicating binary assets in the Space repository.## Run locally```bashgit clone https://github.com/LabMingzeChen/StreetSceneSegmentation.gitcd StreetSceneSegmentationpython -m venv .venvsource .venv/bin/activatepip install -r requirements.txtpython app.py```Open the local URL printed by Gradio. The first prediction downloads the publicmodel weights; no application secret is required.## Use the APIEvery deployed Gradio Space also exposes an endpoint named `segment`:```pythonfrom gradio_client import Client, handle_fileclient = Client("Mingze/StreetSceneSegmentation")result = client.predict(handle_file("street.jpg"),0.55, # overlay opacity0.10, # minimum table share (%)api_name="/segment",)```The Space's **Use via API** panel generates additional Python, JavaScript, andcommand-line examples for the current deployment.## Deploy your own Hugging Face Space1. Create a new **Gradio Space**.2. Select ZeroGPU when it is available for your account, or choose another supported hardware tier.3. Clone this repository and add the Space as a second remote.4. Push the same `main` branch to Hugging Face.```bashgit remote add space https://huggingface.co/spaces/<USERNAME>/<SPACE_NAME>git push -u space main```The YAML block at the top of this README is the Space configuration. Keep`app_file: app.py` and the declared Gradio version aligned with[`requirements.txt`](requirements.txt).## Project structure```textStreetSceneSegmentation/├── app.py Gradio interface and model inference├── segmentation_utils.py Palette, rendering, resizing, and CSV helpers├── tests/ Lightweight image-processing tests└── requirements.txt Reproducible runtime dependencies```## Limitations and responsible use- The model was trained for road-driving scenes and may perform poorly on unusualviewpoints, night images, severe weather, indoor scenes, or locations unlikethe training data.- Thin structures and object boundaries are common sources of pixel-level error.- Results are estimates, not ground truth. Do not use them for navigation,safety-critical decisions, legal judgments, or identifying individuals.- Uploaded images are processed by the selected Hugging Face Space runtime.Avoid submitting private or sensitive imagery to a public service.- Model-license and dataset terms remain governed by the linked model card and[Cityscapes](https://www.cityscapes-dataset.com/) terms.## References- Enze Xie et al., [“SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers”](https://arxiv.org/abs/2105.15203), NeurIPS 2021.- Marius Cordts et al., [“The Cityscapes Dataset for Semantic Urban Scene Understanding”](https://arxiv.org/abs/1604.01685), CVPR 2016.- NVIDIA, [SegFormer-B0 Cityscapes model card](https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024).## CitationIf this software supports your teaching or research, cite the repository:```bibtex@software{chen2026streetscenesegmentation,author = {Chen, Mingze},title = {Street Scene Semantic Segmentation},year = {2026},url = {https://github.com/LabMingzeChen/StreetSceneSegmentation}}```## LicenseReleased under the [MIT License](LICENSE).---<div align="center">**If this project is useful, please star the GitHub repository and share the live Space with your students.**</div>
36 unmodified lines37383940414285 unmodified lines1281291301311321332 unmodified lines1361371381391401411421436 unmodified lines15015115215315415535 unmodified lines19119219319419519619719819936 unmodified linesMODEL_ID = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"OUTPUT_ROOT = Path("/tmp/street-scene-segmentation")@lru_cache(maxsize=1)85 unmodified lines.hero {text-align: center; margin: 0 auto 1rem;}.hero h1 {font-size: 2.1rem; margin-bottom: .3rem;}.muted {color: #64748b;}"""2 unmodified lines"""<div class="hero"><h1>🚦 Street Scene Semantic Segmentation</h1><p>Upload a road or urban street image to identify Cityscapes classes such as roads, sidewalks, buildings, vegetation, vehicles, and people.</p><p class="muted">SegFormer-B0 · 19 street-scene classes · CPU/GPU adaptive</p></div>""")6 unmodified linesheight=470,sources=["upload", "clipboard", "webcam"],)with gr.Accordion("Display settings", open=False):opacity_input = gr.Slider(0.15,35 unmodified linesgr.Markdown("""> The model is downloaded from Hugging Face on the first run and then reused from the container cache.> Images are resized to a maximum side length of 2048 px to prevent memory issues.> This Cityscapes model is intended for street scenes, not medical, remote-sensing, or indoor imagery.""")36 unmodified lines373839404142434445464748495085 unmodified lines1361371381391401411421431441451461471481491501511521532 unmodified lines1561571581591601611621631641651661671686 unmodified lines17517617717817918018118218318418518618735 unmodified lines22322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226336 unmodified linesMODEL_ID = "nvidia/segformer-b0-finetuned-cityscapes-1024-1024"OUTPUT_ROOT = Path("/tmp/street-scene-segmentation")SAMPLE_ROOT = ("https://raw.githubusercontent.com/""LabMingzeChen/HNIVision/main/space/examples")SAMPLE_IMAGES = [f"{SAMPLE_ROOT}/{filename}"for filename in ("140546.jpg", "145541.jpg", "148831.jpg", "160710.jpg")]@lru_cache(maxsize=1)85 unmodified lines.hero {text-align: center; margin: 0 auto 1rem;}.hero h1 {font-size: 2.1rem; margin-bottom: .3rem;}.muted {color: #64748b;}.project-links {display: flex; justify-content: center; gap: .55rem; flex-wrap: wrap; margin-top: .75rem;}.project-link {display: inline-block; padding: .42rem .78rem; border: 1px solid #d7deea;border-radius: 999px; color: inherit !important; text-decoration: none !important;background: white; font-size: .92rem; font-weight: 600;}.project-link:hover {border-color: #6366f1; box-shadow: 0 2px 8px rgba(99, 102, 241, .12);}.guide-grid {display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .8rem; margin: .8rem 0;}.guide-card {border: 1px solid #e2e8f0; border-radius: 12px; padding: .85rem 1rem; background: rgba(255,255,255,.55);}.guide-card h3 {margin: 0 0 .35rem; font-size: 1rem;}.guide-card p {margin: 0; color: #475569; font-size: .93rem; line-height: 1.45;}@media (max-width: 760px) {.guide-grid {grid-template-columns: 1fr;}}"""2 unmodified lines"""<div class="hero"><h1>🚦 Street Scene Semantic Segmentation</h1><p>Turn a street image into a pixel-level map of roads, sidewalks, buildings, vegetation, vehicles, people, and more.</p><p class="muted">SegFormer-B0 · 19 Cityscapes classes · browser-based · no API key required</p><div class="project-links"><a class="project-link" href="https://huggingface.co/spaces/Mingze/StreetSceneSegmentation" target="_blank">🤗 Hugging Face Space</a><a class="project-link" href="https://huggingface.co/nvidia/segformer-b0-finetuned-cityscapes-1024-1024" target="_blank">🧠 Model card</a><a class="project-link" href="https://github.com/LabMingzeChen/StreetSceneSegmentation" target="_blank">⭐ GitHub source</a></div></div>""")6 unmodified linesheight=470,sources=["upload", "clipboard", "webcam"],)if SAMPLE_IMAGES:gr.Examples(examples=SAMPLE_IMAGES,inputs=image_input,label="Or try a sample street scene",examples_per_page=4,)with gr.Accordion("Display settings", open=False):opacity_input = gr.Slider(0.15,35 unmodified linesgr.Markdown("""## How to use the app<div class="guide-grid"><div class="guide-card"><h3>1 · Choose an image</h3><p>Upload, paste, use a webcam, or select one of the sample street scenes above.</p></div><div class="guide-card"><h3>2 · Run segmentation</h3><p>The model assigns one Cityscapes class to every pixel. The first run may take longer while the model loads.</p></div><div class="guide-card"><h3>3 · Explore and download</h3><p>Compare the overlay and mask, review area shares, and download reusable PNG and CSV outputs.</p></div></div>## What the results mean- **Overlay** blends the prediction with the original photograph. White lines mark boundaries between predicted classes.- **Color mask** shows only the standard Cityscapes colors, making classes easier to compare across images.- **Area share** is the percentage of image pixels assigned to each class. It describes visual coverage, not physical land area.- **Class-ID PNG** stores the numeric class at each pixel; the CSV summarizes visible classes and their colors.| Scene layer | Classes ||---|---|| Travel surfaces | road, sidewalk || Built environment | building, wall, fence, pole, traffic light, traffic sign || Nature and sky | vegetation, terrain, sky || People | person, rider || Transport | car, truck, bus, train, motorcycle, bicycle |## Classroom and research ideas- Compare vegetation and built-environment shares across several streets.- Discuss why a pixel classifier can confuse terrain, sidewalk, wall, or fence at object boundaries.- Export the CSV files and create a class-coverage chart for an urban-design exercise.- Compare the same location across seasons, weather conditions, or camera viewpoints.> **Important:** predictions are model estimates, not ground truth. The model was trained for road-driving imagery and can be less reliable for unusual viewpoints, severe weather, night scenes, indoor images, or places unlike the training data. Do not use it for safety-critical decisions or to identify individuals.[Read the SegFormer paper](https://arxiv.org/abs/2105.15203) ·[Explore the Cityscapes dataset](https://www.cityscapes-dataset.com/) ·[View the source on GitHub](https://github.com/LabMingzeChen/StreetSceneSegmentation)""")
LICENSEAdded+21
README.md+183-24
app.py+69-5