File size: 8,919 Bytes
707dfbd
 
 
 
58a221d
707dfbd
 
 
 
 
 
 
 
58a221d
 
707dfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a221d
707dfbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Processing imscdr_ac_in___8.png...\n",
      "Status Code: 500\n",
      "Response Text: {\"detail\":\"conv2d() received an invalid combination of arguments - got (str, Parameter, NoneType, tuple, tuple, tuple, int), but expected one of:\\n * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, tuple of ints padding, tuple of ints dilation, int groups)\\n      didn't match because some of the arguments have invalid types: (!str!, !Parameter!, !NoneType!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)\\n * (Tensor input, Tensor weight, Tensor bias, tuple of ints stride, str padding, tuple of ints dilation, int groups)\\n      didn't match because some of the arguments have invalid types: (!str!, !Parameter!, !NoneType!, !tuple of (int, int)!, !tuple of (int, int)!, !tuple of (int, int)!, int)\\n\"}\n",
      "Finished processing imscdr_ac_in___8.png in 10.38 seconds\n",
      "Processing imscdr_ac_in___9.png...\n",
      "Status Code: 500\n",
      "Response Text: <!DOCTYPE html>\n",
      "<html class=\"\">\n",
      "<head>\n",
      "    <meta charset=\"utf-8\"/>\n",
      "    <meta\n",
      "            name=\"viewport\"\n",
      "            content=\"width=device-width, initial-scale=1.0, user-scalable=no\"\n",
      "    />\n",
      "    <meta\n",
      "            name=\"description\"\n",
      "            content=\"We’re on a journey to advance and democratize artificial intelligence through open source and open science.\"\n",
      "    />\n",
      "    <meta property=\"fb:app_id\" content=\"1321688464574422\"/>\n",
      "    <meta name=\"twitter:card\" content=\"summary_large_image\"/>\n",
      "    <meta name=\"twitter:site\" content=\"@huggingface\"/>\n",
      "    <meta\n",
      "            property=\"og:title\"\n",
      "            content=\"Hugging Face – The AI community building the future.\"\n",
      "    />\n",
      "    <meta property=\"og:type\" content=\"website\"/>\n",
      "\n",
      "    <title>Hugging Face – The AI community building the future.</title>\n",
      "    <style>\n",
      "        body {\n",
      "            margin: 0;\n",
      "        }\n",
      "\n",
      "        main {\n",
      "            background-color: white;\n",
      "            min-height: 100vh;\n",
      "            padding: 7rem 1rem 8rem 1rem;\n",
      "            text-align: center;\n",
      "            font-family: Source Sans Pro, ui-sans-serif, system-ui, -apple-system,\n",
      "            BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans,\n",
      "            sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol,\n",
      "            Noto Color Emoji;\n",
      "        }\n",
      "\n",
      "        img {\n",
      "            width: 6rem;\n",
      "            height: 6rem;\n",
      "            margin: 0 auto 1rem;\n",
      "        }\n",
      "\n",
      "        h1 {\n",
      "            font-size: 3.75rem;\n",
      "            line-height: 1;\n",
      "            color: rgba(31, 41, 55, 1);\n",
      "            font-weight: 700;\n",
      "            box-sizing: border-box;\n",
      "            margin: 0 auto;\n",
      "        }\n",
      "\n",
      "        p {\n",
      "            color: rgba(107, 114, 128, 1);\n",
      "            font-size: 1.125rem;\n",
      "            line-height: 1.75rem;\n",
      "            max-width: 28rem;\n",
      "            box-sizing: border-box;\n",
      "            margin: 0 auto;\n",
      "        }\n",
      "\n",
      "        .dark main {\n",
      "            background-color: rgb(11, 15, 25);\n",
      "        }\n",
      "\n",
      "        .dark h1 {\n",
      "            color: rgb(209, 213, 219);\n",
      "        }\n",
      "\n",
      "        .dark p {\n",
      "            color: rgb(156, 163, 175);\n",
      "        }\n",
      "    </style>\n",
      "    <script>\n",
      "        // On page load or when changing themes, best to add inline in `head` to avoid FOUC\n",
      "        const key = \"_tb_global_settings\";\n",
      "        let theme = window.matchMedia(\"(prefers-color-scheme: dark)\").matches\n",
      "            ? \"dark\"\n",
      "            : \"light\";\n",
      "        try {\n",
      "            const storageTheme = JSON.parse(window.localStorage.getItem(key)).theme;\n",
      "            if (storageTheme) {\n",
      "                theme = storageTheme === \"dark\" ? \"dark\" : \"light\";\n",
      "            }\n",
      "        } catch (e) {\n",
      "        }\n",
      "        if (theme === \"dark\") {\n",
      "            document.documentElement.classList.add(\"dark\");\n",
      "        } else {\n",
      "            document.documentElement.classList.remove(\"dark\");\n",
      "        }\n",
      "    </script>\n",
      "</head>\n",
      "\n",
      "<body>\n",
      "<main>\n",
      "    <img\n",
      "            src=\"https://huggingface.co/front/assets/huggingface_logo.svg\"\n",
      "            alt=\"\"\n",
      "    />\n",
      "    <div>\n",
      "        <h1>500</h1>\n",
      "        <p>Sorry, there is an error on our side.</p>\n",
      "    </div>\n",
      "</main>\n",
      "</body>\n",
      "</html>\n",
      "\n",
      "Finished processing imscdr_ac_in___9.png in 11.36 seconds\n",
      "{}\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import requests\n",
    "import base64\n",
    "from PIL import Image\n",
    "import time\n",
    "\n",
    "api_url = \"https://banao-tech-omniapi.hf.space/process_image\"\n",
    "\n",
    "def process_image_folder(input_folder, output_folder, box_threshold=0.03, iou_threshold=0.1):\n",
    "    if not os.path.exists(output_folder):\n",
    "        os.makedirs(output_folder)\n",
    "\n",
    "    results = {}\n",
    "    image_files = [f for f in os.listdir(input_folder) if f.lower().endswith(('.jpg', '.jpeg', '.png'))]\n",
    "\n",
    "    for image_file in image_files:\n",
    "        image_path = os.path.join(input_folder, image_file)\n",
    "        print(f\"Processing {image_file}...\")\n",
    "\n",
    "        start_time = time.time()\n",
    "\n",
    "        try:\n",
    "            with open(image_path, \"rb\") as img_file:\n",
    "                files = {\"image_file\": img_file}\n",
    "                data = {\n",
    "                    \"box_threshold\": box_threshold,\n",
    "                    \"iou_threshold\": iou_threshold,\n",
    "                }\n",
    "                response = requests.post(api_url, files=files, data=data)\n",
    "                print(\"Status Code:\", response.status_code)\n",
    "                print(\"Response Text:\", response.text)\n",
    "\n",
    "            if response.status_code == 200:\n",
    "                result = response.json()\n",
    "                results[image_file] = {\n",
    "                    \"parsed_content_list\": result.get(\"parsed_content_list\", []),\n",
    "                    \"label_coordinates\": result.get(\"label_coordinates\", []),\n",
    "                }\n",
    "                output_image_data = base64.b64decode(result[\"image\"])\n",
    "                output_image_path = os.path.join(output_folder, f\"labeled_{image_file}\")\n",
    "                with open(output_image_path, \"wb\") as img_file:\n",
    "                    img_file.write(output_image_data)\n",
    "\n",
    "        except Exception as e:\n",
    "            print(f\"Error processing {image_file}: {e}\")\n",
    "\n",
    "        end_time = time.time()\n",
    "        print(f\"Finished processing {image_file} in {end_time - start_time:.2f} seconds\")\n",
    "\n",
    "    return results\n",
    "\n",
    "# # Example call\n",
    "res = process_image_folder('test', 'output_folder')\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "audit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}