thrinadhn commited on
Commit
39e6403
·
verified ·
1 Parent(s): 0d59b2b

Update experiments.py

Browse files
Files changed (1) hide show
  1. experiments.py +155 -0
experiments.py CHANGED
@@ -4,6 +4,161 @@ import phoenix_helpers
4
  import helpers
5
 
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  load_dotenv()
8
  models = helpers.fetch_models()
9
 
 
4
  import helpers
5
 
6
 
7
+
8
+ import os
9
+ abspath = os.path.abspath('') ## String which contains absolute path to the script file
10
+ os.chdir(abspath) ## Setting up working directory
11
+ import copy
12
+
13
+ from transformers import AutoProcessor, AutoModelForCausalLM
14
+ from PIL import Image
15
+ import requests
16
+ import utils
17
+ from PIL import Image, ImageDraw, ImageFont
18
+
19
+ image = Image.open(r"C:\Users\thrin\Downloads\rohitha\floerence.png").convert("RGB")
20
+
21
+
22
+ model_id = 'microsoft/Florence-2-large'
23
+ model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True).eval().cuda()
24
+ processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
25
+
26
+ utils.set_model_info(model, processor)
27
+
28
+ image
29
+
30
+ img_list = ["/content/images_ocr_image_3.jpg",
31
+ "/content/images_ocr_image_2.jpg",
32
+ "/content/images_ocr_image_1.jpg",
33
+ "/content/images_ocr_image_4.png",
34
+ "/content/pci complliance and Credit card Authorization forms Hand written - Copy.jpg",
35
+ "/content/Charlemagne.png",
36
+ "/content/Frederick II.png",
37
+ "/content/Henry VIII.png",
38
+ "/content/Louis XIV.png",
39
+ "/content/William IV.png",
40
+ "/content/pci complliance and Credit card Authorization forms Hand written - Copy.jpg"
41
+ ]
42
+ img_list = [r"C:\Users\thrin\Downloads\rohitha\floerence.png",
43
+ r"C:\Users\thrin\Downloads\rohitha\credit-card-auth-form-xout.jpg"]
44
+
45
+ for item in img_list:
46
+ path = item
47
+ print("<================>")
48
+ image = Image.open(path)
49
+ image_rgb = Image.open(path).convert("RGB")
50
+
51
+ tasks = [utils.TaskType.CAPTION,
52
+ utils.TaskType.DETAILED_CAPTION,
53
+ utils.TaskType.MORE_DETAILED_CAPTION,]
54
+
55
+ for task in tasks:
56
+ results = utils.run_example(task, image_rgb)
57
+ print(f'{task.value}{results[task]}')
58
+
59
+ task = utils.TaskType.OCR
60
+ results = utils.run_example(task, image_rgb)
61
+ print('Detected Text: ', results[task])
62
+
63
+
64
+ task = utils.TaskType.OCR
65
+ results = utils.run_example(task, image_rgb)
66
+ print('Detected Text: ', results[task])
67
+
68
+
69
+ task = utils.TaskType.OCR_WITH_REGION
70
+ results = utils.run_example(task, image_rgb)
71
+
72
+ # Boxes drawn directly to image, so copy to avoid adulterating image for later tasks
73
+ image_copy = copy.deepcopy(image)
74
+ utils.draw_ocr_bboxes(image_copy, results[task])
75
+
76
+
77
+
78
+ task = utils.TaskType.OCR
79
+ results = utils.run_example(task, image_rgb)
80
+ print('Detected Text: ', results[task])
81
+
82
+ task = utils.TaskType.OCR_WITH_REGION
83
+ results = utils.run_example(task, image_rgb)
84
+ print('Detected Text: ', results[task])
85
+
86
+ counter = 0
87
+ for item in img_list:
88
+ path = item
89
+ print("<======>",counter,"<======>")
90
+ image = Image.open(path)
91
+ image_rgb = Image.open(path).convert("RGB")
92
+
93
+ tasks = [utils.TaskType.CAPTION,
94
+ utils.TaskType.DETAILED_CAPTION,
95
+ utils.TaskType.MORE_DETAILED_CAPTION,]
96
+
97
+ for task in tasks:
98
+ results = utils.run_example(task, image_rgb)
99
+ print(counter,"<======>",f'{task.value}{results[task]}')
100
+
101
+ task = utils.TaskType.OCR
102
+ results = utils.run_example(task, image_rgb)
103
+ print(counter,"<======>",'Detected Text: ', results[task])
104
+
105
+ task = utils.TaskType.OCR_WITH_REGION
106
+ results = utils.run_example(task, image_rgb)
107
+ print(counter,"<======>",'Detected Text: ', results[task])
108
+
109
+
110
+ results[task].items()
111
+
112
+ results[task].keys()
113
+
114
+ len(results[task]["quad_boxes"])
115
+ len(results[task]["labels"])
116
+
117
+ OCR = '<OCR>'
118
+ """ OCR for entire image """
119
+ OCR_WITH_REGION = '<OCR_WITH_REGION>'
120
+
121
+ from PIL import ImageDraw, Image
122
+
123
+ {'quad_boxes': [
124
+ [78.91199493408203,
125
+ 249.8040008544922,
126
+ 332.35198974609375,
127
+ 251.7480010986328,
128
+ 332.35198974609375,
129
+ 299.70001220703125,
130
+ 78.91199493408203,
131
+ 297.7560119628906]
132
+ ],
133
+ 'labels': ['</s>3702692432']}
134
+
135
+ def draw_multiple_bounding_boxes(image, coords_and_labels):
136
+ draw = ImageDraw.Draw(image)
137
+ width, height = image.size
138
+ for obj in coords_and_labels:
139
+ # Extract the bounding box coordinates
140
+ y1, x1, y2, x2 = obj['bbox'][0] * height, obj['bbox'][1] * width, obj['bbox'][2] * height, obj['bbox'][3] * width
141
+ # Draw bounding box and label
142
+ draw.rectangle([x1, y1, x2, y2], outline="red", width=3)
143
+ draw.text((x1, y1), obj['label'], fill="red")
144
+ return image
145
+
146
+ image = r"C:\Users\thrin\Downloads\rohitha\floerence.png"
147
+ image = Image.open(image)
148
+
149
+ quad_box = [(78.91199493408203,249.8040008544922),
150
+ (332.35198974609375,251.7480010986328),
151
+ (332.35198974609375,299.70001220703125),
152
+ (78.91199493408203,297.7560119628906)]
153
+
154
+ draw = ImageDraw.Draw(image)
155
+
156
+ draw.polygon(quad_box,outline="green",width=2)
157
+ image.show()
158
+ image.save(r"C:\Users\thrin\Downloads\florence-2-master (1)\florence-2-master/florence_bounding.png")
159
+ #
160
+
161
+
162
  load_dotenv()
163
  models = helpers.fetch_models()
164