karthickn commited on
Commit
94ff611
1 Parent(s): 069b485

Upload 6 files

Browse files
Files changed (7) hide show
  1. .gitattributes +1 -0
  2. app.py +65 -0
  3. color.py +36 -0
  4. color_wheel.py +146 -0
  5. examples/image1.jpg +0 -0
  6. examples/image2.jpg +3 -0
  7. requirements.txt +6 -0
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ examples/image2.jpg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import DetrImageProcessor, DetrForObjectDetection
4
+ from color import Color
5
+ from color_wheel import ColorWheel
6
+ from PIL import ImageDraw, ImageFont
7
+
8
+ processor = DetrImageProcessor.from_pretrained('facebook/detr-resnet-50')
9
+ model = DetrForObjectDetection.from_pretrained('facebook/detr-resnet-50')
10
+
11
+ def process_image(image, margin):
12
+ if image is None:
13
+ yield [None, None, None]
14
+ return
15
+
16
+ color = Color.fromRgb(0xff, 0x0, 0x0)
17
+ cwt = ColorWheel(color)
18
+ colors = []
19
+ for t in cwt.tone15:
20
+ cw = ColorWheel(Color.fromRgb(t.r, t.g, t.b))
21
+ for h in cw.hue:
22
+ colors.append((h.r, h.g, h.b))
23
+
24
+ inputs = processor(images=image, return_tensors='pt')
25
+ outputs = model(**inputs)
26
+ target_sizes = torch.tensor([image.size[::-1]])
27
+ results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
28
+
29
+ index = 0
30
+ gallery = []
31
+ labels = []
32
+ drawImage = image.copy()
33
+ draw = ImageDraw.Draw(drawImage)
34
+ for score, label, box in zip(results['scores'], results['labels'], results['boxes']):
35
+ if index >= len(colors):
36
+ break
37
+ box = [round(i) for i in box.tolist()]
38
+ box[0] = max(0, box[0] - margin)
39
+ box[1] = max(0, box[1] - margin)
40
+ box[2] = min(image.width, box[2] + margin)
41
+ box[3] = min(image.height, box[3] + margin)
42
+ draw.rectangle([(box[0], box[1]), (box[2], box[3])], outline=colors[index], width=4)
43
+ gallery.append(image.crop((box[0], box[1], box[2], box[3])))
44
+ labels.append(model.config.id2label[label.item()])
45
+ index += 1
46
+ yield [drawImage, gallery, ','.join(labels)]
47
+
48
+ app = gr.Interface(
49
+ title='Object Detection for Image',
50
+ fn=process_image,
51
+ inputs=[
52
+ gr.Image(type='pil'),
53
+ gr.Slider(maximum=100, step=1, label='margin'),
54
+ ],
55
+ outputs=[
56
+ gr.Image(label='boxes', type='pil'),
57
+ gr.Gallery(label='gallery', columns=8, height=140),
58
+ gr.Textbox(label='text'),
59
+ ],
60
+ allow_flagging='never',
61
+ examples=[['examples/image1.jpg', 0], ['examples/image2.jpg', 0]],
62
+ #cache_examples=False
63
+ )
64
+ app.queue(concurrency_count=20)
65
+ app.launch()
color.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class Color:
2
+ @property
3
+ def a(self):
4
+ return self._a
5
+
6
+ @property
7
+ def r(self):
8
+ return self._r
9
+
10
+ @property
11
+ def g(self):
12
+ return self._g
13
+
14
+ @property
15
+ def b(self):
16
+ return self._b
17
+
18
+ def __init__(self, a, r, g, b):
19
+ self._a = a
20
+ self._r = r
21
+ self._g = g
22
+ self._b = b
23
+
24
+ @staticmethod
25
+ def fromArgb(a, r, g, b):
26
+ return Color(a, r, g, b)
27
+
28
+ @staticmethod
29
+ def fromRgb(r, g, b):
30
+ return Color(0xff, r, g, b)
31
+
32
+ def __eq__(self, other):
33
+ return self.a == other.a and self.r == other.r and self.g == other.g and self.b == other.b
34
+
35
+ def __str__(self):
36
+ return 'ARGB({},{},{},{})'.format(self.a, self.r, self.g, self.b)
color_wheel.py ADDED
@@ -0,0 +1,146 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ from color import Color
3
+
4
+ class ColorWheel:
5
+ @property
6
+ def baseColor(self):
7
+ return self._baseColor
8
+
9
+ @property
10
+ def hue(self):
11
+ return [
12
+ self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12), self.addH(3.0 / 12), self.addH(4.0 / 12), self.addH(5.0 / 12),
13
+ self.addH(-6.0 / 12), self.addH(-5.0 / 12), self.addH(-4.0 / 12), self.addH(-3.0 / 12), self.addH(-2.0 / 12), self.addH(-1.0 / 12)
14
+ ]
15
+
16
+ @property
17
+ def tone(self):
18
+ return [self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16)]
19
+
20
+ @property
21
+ def tone15(self):
22
+ return [self.addWhite(-7.0 / 16), self.addWhite(-6.0 / 16), self.addWhite(-5.0 / 16), self.addWhite(-4.0 / 16), self.addWhite(-3.0 / 16),
23
+ self.addWhite(-2.0 / 16), self.addWhite(-1.0 / 16), self.baseColor, self.addWhite(1.0 / 16), self.addWhite(2.0 / 16),
24
+ self.addWhite(3.0 / 16), self.addWhite(4.0 / 16), self.addWhite(5.0 / 16), self.addWhite(6.0 / 16), self.addWhite(7.0 / 16)]
25
+
26
+ @property
27
+ def complementaryColors(self):
28
+ return [self.baseColor, self.addH(0.5)]
29
+
30
+ @property
31
+ def triadicColors(self):
32
+ return [self.addH(-4.0 / 12), self.baseColor, self.addH(4.0 / 12)]
33
+
34
+ @property
35
+ def splitComplementaryColors(self):
36
+ return [self.addH(-5.0 / 12), self.baseColor, self.addH(5.0 / 12)]
37
+
38
+ @property
39
+ def analogousColors(self):
40
+ return [self.addH(-2.0 / 12), self.addH(-1.0 / 12), self.baseColor, self.addH(1.0 / 12), self.addH(2.0 / 12)]
41
+
42
+ def __init__(self, c):
43
+ self._baseColor = c
44
+ self._r = c.r / 255.0
45
+ self._g = c.g / 255.0
46
+ self._b = c.b / 255.0
47
+
48
+ @staticmethod
49
+ def fromHsv(h, s, v):
50
+ r, g, b = ColorWheel.hsvToRgb(h, s, v)
51
+ c = Color.fromRgb(round(r * 255), round(g * 255), round(b * 255))
52
+ return ColorWheel(c)
53
+
54
+ def addWhite(self, value):
55
+ r, g, b = (self._r + value, self._g + value, self._b + value)
56
+ r = min(max(r, 0.0), 1.0)
57
+ g = min(max(g, 0.0), 1.0)
58
+ b = min(max(b, 0.0), 1.0)
59
+ return self._fromRgb(r, g, b)
60
+
61
+ def addH(self, value):
62
+ h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
63
+ h = (h + value) % 1.0
64
+ if h < 0.0:
65
+ h += 1.0
66
+ r, g, b = ColorWheel.hsvToRgb(h, s, v)
67
+ return self._fromRgb(r, g, b)
68
+
69
+ def addS(self, value):
70
+ h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
71
+ s += value
72
+ s = min(max(s, 0.0), 1.0)
73
+ r, g, b = ColorWheel.hsvToRgb(h, s, v)
74
+ return self._fromRgb(r, g, b)
75
+
76
+ def addV(self, value):
77
+ h, s, v = ColorWheel.rgbToHsv(self._r, self._g, self._b)
78
+ v += value
79
+ v = min(max(v, 0.0), 1.0)
80
+ r, g, b = ColorWheel.hsvToRgb(h, s, v)
81
+ return self._fromRgb(r, g, b)
82
+
83
+ @staticmethod
84
+ def rgbToHsv(r, g, b):
85
+ if r < 0.0 or r > 1.0:
86
+ raise ValueError()
87
+ if g < 0.0 or g > 1.0:
88
+ raise ValueError()
89
+ if b < 0.0 or b > 1.0:
90
+ raise ValueError()
91
+ cmax = max(r, g, b)
92
+ cmin = min(r, g, b)
93
+ h = cmax - cmin
94
+ if h > 0.0:
95
+ if cmax == r:
96
+ h = (g - b) / h
97
+ if h < 0.0:
98
+ h += 6.0
99
+ elif cmax == g:
100
+ h = 2.0 + (b - r) / h
101
+ else:
102
+ h = 4.0 + (r - g) / h
103
+ h /= 6.0
104
+ s = cmax - cmin
105
+ if cmax != 0.0:
106
+ s /= cmax
107
+ v = cmax
108
+ return h, s, v
109
+
110
+ @staticmethod
111
+ def hsvToRgb(h, s, v):
112
+ if h < 0.0 or h > 1.0:
113
+ raise ValueError()
114
+ if s < 0.0 or s > 1.0:
115
+ raise ValueError()
116
+ if v < 0.0 or v > 1.0:
117
+ raise ValueError()
118
+ r = v
119
+ g = v
120
+ b = v
121
+ if s > 0.0:
122
+ h *= 6.0
123
+ i = math.floor(h)
124
+ f = h - i
125
+ if i == 1:
126
+ r *= 1 - s * f
127
+ b *= 1 - s
128
+ elif i == 2:
129
+ r *= 1 - s
130
+ b *= 1 - s * (1 - f)
131
+ elif i == 3:
132
+ r *= 1 - s
133
+ g *= 1 - s * f
134
+ elif i == 4:
135
+ r *= 1 - s * (1 - f)
136
+ g *= 1 - s
137
+ elif i == 5:
138
+ g *= 1 - s
139
+ b *= 1 - s * f
140
+ else:
141
+ g *= 1 - s * (1 - f)
142
+ b *= 1 - s
143
+ return r, g, b
144
+
145
+ def _fromRgb(self, r, g, b):
146
+ return Color.fromArgb(self.baseColor.a, round(r * 255), round(g * 255), round(b * 255))
examples/image1.jpg ADDED
examples/image2.jpg ADDED

Git LFS Details

  • SHA256: 1437167a9def854d03dcb2651ce012ffdf3b8a80c4d13b36b379e3148d33f2c8
  • Pointer size: 132 Bytes
  • Size of remote file: 3.97 MB
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ torchvision
4
+ torchaudio
5
+ transformers
6
+ timm