graceliying82 commited on
Commit
c6b4b61
·
1 Parent(s): 062938d

object detection

Browse files
.gitignore ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ pip-wheel-metadata/
24
+ share/python-wheels/
25
+ *.egg-info/
26
+ .installed.cfg
27
+ *.egg
28
+
29
+ # Interpreter
30
+ .env
31
+
32
+ # Unit test / coverage reports
33
+ .coverage
34
+ .tox
35
+ nosetests.xml
36
+ coverage.xml
37
+ *.cover
38
+ .hypothesis/
39
+
40
+ # Translations
41
+ *.mo
42
+ *.pot
43
+
44
+ # Django stuff:
45
+ *.log
46
+ local_settings.py
47
+ db.sqlite3
48
+ db.sqlite3-journal
49
+
50
+ # Flask stuff:
51
+ instance/
52
+ .webassets-cache
53
+
54
+ # Scrapy stuff:
55
+ .scrapy
56
+
57
+ # Sphinx documentation
58
+ docs/_build/
59
+
60
+ # PyBuilder
61
+ target/
62
+
63
+ # Jupyter Notebook
64
+ .ipynb_checkpoints
65
+
66
+ # IPython
67
+ profile_default/
68
+ ipython_config.py
69
+
70
+ # pyenv
71
+ .python-version
72
+
73
+ # pipenv
74
+ Pipfile.lock
75
+
76
+ # PyCharm
77
+ .idea/
78
+ .idea/*
79
+ /object-detection/flagged/log.csv
80
+ /object-detection/flagged/
Pipfile ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [[source]]
2
+ url = "https://pypi.org/simple"
3
+ verify_ssl = true
4
+ name = "pypi"
5
+
6
+ [packages]
7
+ requests = "*"
8
+ langchain = "*"
9
+ black = "*"
10
+ openai = "*"
11
+ install = "*"
12
+ google-search-results = "*"
13
+ tweepy = "*"
14
+ flask = "*"
15
+ langchain-openai = "*"
16
+ python-dotenv = "*"
17
+ langchain-community = "*"
18
+ pydantic = "==v1.10.13"
19
+ langchain-core = "*"
20
+ langchainhub = "*"
21
+ pinecone-client = "*"
22
+ gradio = "*"
23
+ timm = "*"
24
+ inflect = "*"
25
+ phonemizer = "*"
26
+ py-espeak-ng = "*"
27
+ transformers = "*"
28
+
29
+ [dev-packages]
30
+
31
+ [requires]
32
+ python_version = "3.11"
object-detection/__init__.py ADDED
File without changes
object-detection/object_detection.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import DetrImageProcessor, DetrForObjectDetection
2
+ import torch
3
+ from PIL import Image, ImageDraw, ImageFont
4
+ import gradio as gr
5
+
6
+
7
+ def render_result_in_image(image):
8
+ """
9
+ Render detected objects in the input image.
10
+
11
+ Args:
12
+ image (PIL.Image): Input image.
13
+
14
+ Returns:
15
+ PIL.Image: Image with bounding boxes and labels drawn.
16
+ """
17
+ processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
18
+ model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50", revision="no_timm")
19
+
20
+ inputs = processor(images=image, return_tensors="pt")
21
+ outputs = model(**inputs)
22
+
23
+ # convert outputs (bounding boxes and class logits) to COCO API
24
+ # let's only keep detections with score > 0.9
25
+ target_sizes = torch.tensor([image.size[::-1]])
26
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
27
+
28
+ # Create id to label mapping
29
+ id2label = {idx: model.config.id2label[idx] for idx in range(len(model.config.id2label))}
30
+
31
+ # Render results in the image
32
+ rendered_image = render_result_in_image_helper(image.copy(), results, id2label)
33
+ return rendered_image
34
+
35
+
36
+ def render_result_in_image_helper(image, results, id2label):
37
+ """
38
+ Helper function to render detected objects in the input image.
39
+
40
+ Args:
41
+ image (PIL.Image): Input image.
42
+ results (dict): Detection results containing 'scores', 'labels', and 'boxes'.
43
+ id2label (dict): Mapping from class indices to class labels.
44
+
45
+ Returns:
46
+ PIL.Image: Image with bounding boxes and labels drawn.
47
+ """
48
+ draw = ImageDraw.Draw(image)
49
+ font = ImageFont.load_default()
50
+
51
+ for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
52
+ # Draw bounding box
53
+ draw.rectangle(box.tolist(), outline="red", width=20)
54
+
55
+ # Draw label
56
+ label_text = f"{id2label[label.item()]}: {score:.4f}"
57
+ draw.text((box[0], box[1]), label_text, fill="white", font=font)
58
+
59
+ return image
60
+
61
+
62
+ demo = gr.Interface(
63
+ fn=render_result_in_image,
64
+ inputs=gr.inputs.Image(type="pil", label="Upload Image"),
65
+ outputs=gr.outputs.Image(type="pil", label="Output Image with Predicted Instances")
66
+ )
67
+
68
+ demo.launch()