Spaces:
Sleeping
Sleeping
Update utils/basic_functions.py
Browse files- utils/basic_functions.py +14 -0
utils/basic_functions.py
CHANGED
|
@@ -78,6 +78,7 @@ def huggingface_detect_id_box(model_name,url):
|
|
| 78 |
try:
|
| 79 |
#image = get_image(url)
|
| 80 |
image = Image.open(requests.get(url,stream=True).raw)
|
|
|
|
| 81 |
|
| 82 |
system_prompt = f"""
|
| 83 |
You are an AI document processing assistant. Analyze the provided image. Identify the ID number in the document.
|
|
@@ -87,8 +88,10 @@ def huggingface_detect_id_box(model_name,url):
|
|
| 87 |
as well but that looks to be rare. The output should only be a string in the format [x0,y0,x1,y1], and the
|
| 88 |
values should fit into the image size which is {image.size}.
|
| 89 |
"""
|
|
|
|
| 90 |
|
| 91 |
processor,model=load_model(model_name)
|
|
|
|
| 92 |
|
| 93 |
conversation = [
|
| 94 |
{
|
|
@@ -99,8 +102,13 @@ def huggingface_detect_id_box(model_name,url):
|
|
| 99 |
],
|
| 100 |
},
|
| 101 |
]
|
|
|
|
|
|
|
| 102 |
prompt = processor.apply_chat_template(conversation,add_generation_prompt=True)
|
|
|
|
|
|
|
| 103 |
inputs = processor(images=image,text=prompt,return_tensors="pt").to(model.device)
|
|
|
|
| 104 |
|
| 105 |
"""
|
| 106 |
with torch.no_grad():
|
|
@@ -116,12 +124,18 @@ def huggingface_detect_id_box(model_name,url):
|
|
| 116 |
"""
|
| 117 |
|
| 118 |
output = model.generate(**inputs,max_new_tokens=200,do_sample=False)
|
|
|
|
|
|
|
| 119 |
bbox = processor.decode(output[0][2:],skip_special_tokens=True)
|
| 120 |
print(bbox)
|
| 121 |
|
| 122 |
|
| 123 |
draw = ImageDraw.Draw(image)
|
|
|
|
|
|
|
| 124 |
draw.rectangle(bbox,outline="red",width=5)
|
|
|
|
|
|
|
| 125 |
#image.save(f'{os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}\\download\\{dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.jpg')
|
| 126 |
return image,bbox
|
| 127 |
except Exception as e:
|
|
|
|
| 78 |
try:
|
| 79 |
#image = get_image(url)
|
| 80 |
image = Image.open(requests.get(url,stream=True).raw)
|
| 81 |
+
print(image)
|
| 82 |
|
| 83 |
system_prompt = f"""
|
| 84 |
You are an AI document processing assistant. Analyze the provided image. Identify the ID number in the document.
|
|
|
|
| 88 |
as well but that looks to be rare. The output should only be a string in the format [x0,y0,x1,y1], and the
|
| 89 |
values should fit into the image size which is {image.size}.
|
| 90 |
"""
|
| 91 |
+
print(system_prompt)
|
| 92 |
|
| 93 |
processor,model=load_model(model_name)
|
| 94 |
+
print(processor,model)
|
| 95 |
|
| 96 |
conversation = [
|
| 97 |
{
|
|
|
|
| 102 |
],
|
| 103 |
},
|
| 104 |
]
|
| 105 |
+
print(conversation)
|
| 106 |
+
|
| 107 |
prompt = processor.apply_chat_template(conversation,add_generation_prompt=True)
|
| 108 |
+
print(prompt)
|
| 109 |
+
|
| 110 |
inputs = processor(images=image,text=prompt,return_tensors="pt").to(model.device)
|
| 111 |
+
print(inputs)
|
| 112 |
|
| 113 |
"""
|
| 114 |
with torch.no_grad():
|
|
|
|
| 124 |
"""
|
| 125 |
|
| 126 |
output = model.generate(**inputs,max_new_tokens=200,do_sample=False)
|
| 127 |
+
print(output)
|
| 128 |
+
|
| 129 |
bbox = processor.decode(output[0][2:],skip_special_tokens=True)
|
| 130 |
print(bbox)
|
| 131 |
|
| 132 |
|
| 133 |
draw = ImageDraw.Draw(image)
|
| 134 |
+
print(draw)
|
| 135 |
+
|
| 136 |
draw.rectangle(bbox,outline="red",width=5)
|
| 137 |
+
print(image)
|
| 138 |
+
|
| 139 |
#image.save(f'{os.path.dirname(os.path.dirname(os.path.abspath(__file__)))}\\download\\{dt.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")}.jpg')
|
| 140 |
return image,bbox
|
| 141 |
except Exception as e:
|