NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

class DataExtractionView(APIView):
authentication_classes = [TokenAuthentication]
permission_classes = [IsAuthenticated]

def post(self, request, id):
try:
instance = TicketField.objects.get(id=id)
print(instance.id, "Instance")
image_link = instance.tickets_jpeg
instance_id = instance.id
print(image_link)
except TicketField.DoesNotExist:
return Response({"message": "Image does not exist"}, status=status.HTTP_404_NOT_FOUND)

response = requests.get(image_link)
if response.status_code == 200:
# Download the image and save into directory
# image_name = os.path.basename(image_link)
# image_path = os.path.join(images_directory, image_name)
# print(image_path,"!!!!!!!!!!!!!!!!!")
match = re.search(r'([^/]+).jpg', image_link)
filename = f"{match.group(1)}.jpg"
print(filename)
image_path = f"{settings.IMAGES_ROOT}/{instance_id}"
print("IMAGES PATHN",image_path)
os.makedirs(image_path, exist_ok=True)
image_file_path = f"{image_path}/{filename}"
print("IMAGES PATH",image_file_path)
with open(image_file_path, 'wb') as f:
f.write(response.content)

cropped_image_path, cropped_image_dir = get_cropped_images(image_file_path, instance_id)
print('IMAGES PATH', cropped_image_path)

bol_text = ''
outbound_text = ''
inbound_text = ''

# Initialize AWS Textract client
textract_client = boto3.client('textract', region_name=settings.REGION_NAME,
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY)

try:
for image in cropped_image_path:
print('IMAGE', image)
# Read the image
with open(image, 'rb') as file:
file_bytes = file.read()

# Call Textract API to analyze the image
response = textract_client.analyze_document(
Document={'Bytes': file_bytes},
FeatureTypes=['TABLES']
)

# Extract raw text from the response
text = ""
for item in response["Blocks"]:
if item["BlockType"] == "LINE":
text = text + " " + item["Text"]

# Clean the text by removing ambiguity
cleaned_text = text.replace('"', '').replace('\', '')
# print("CLEANED TEXT", cleaned_text)

# Determine which type of image it is based on the image name
image_name = image.split('/')[-1] # Assuming image path contains '/'
if "inbound" in image_name:
inbound_text += cleaned_text
print("INBOUND", inbound_text)
elif "lading" in image_name:
bol_text += cleaned_text
print("LADING", bol_text)
elif "outbound" in image_name:
outbound_text += cleaned_text
print("OUTBOUND", outbound_text)

# Constructing the dictionary
result = {
'BOL': bol_text.strip(),
'OUTBOUND': outbound_text.strip(),
'INBOUND': inbound_text.strip()
}
print("RESULT DICT", result)
print('BOL RAW TEXT', result['BOL'])
print("FORT RAW TEXT", result['INBOUND'])
for key in result:
if 'BOL' in key:
if result['BOL'] != '':
bol_raw_text = result['BOL']
# Calling BOL function to do further processing.
bol_mapped_data = bol(bol_raw_text, instance)
else:
# If bol_raw_text is empty, store None or appropriate value
bol_mapped_data = {}

elif 'OUTBOUND' in key:
if result['OUTBOUND'] != '':
outbound_raw_text = result['OUTBOUND']
# Calling OUTBOUND function to do further processing.
outbound_mapped_data = outbound(outbound_raw_text, instance)
else:
# If outbound_raw_text is empty, store None or appropriate value
outbound_mapped_data = {}

elif 'INBOUND' in key:
if result['INBOUND'] != '':
inbound_raw_text = result['INBOUND']
# Calling INBOUND function to do further processing.
inbound_mapped_data = inbound(inbound_raw_text, instance)
print('INBOUND DATA', inbound_mapped_data)
else:
# If inbound_raw_text is empty, store None or appropriate value
inbound_mapped_data = {}

# Mapping the data of BOL_OUT_IN
bol_out_in = bol_mapped_data.get('bol_number', 'NA') + ', ' + outbound_mapped_data.get(
'outbound_ticket_number', 'NA') + ', ' + inbound_mapped_data.get('inbound_ticket_number', 'NA')
instance.bol_out_in = bol_out_in
instance.save()

context = {
'bol_number': bol_mapped_data.get('bol_number', ''),
'outbound_ticket_number': outbound_mapped_data.get('outbound_ticket_number', ''),
'inbound_ticket_number': inbound_mapped_data.get('inbound_ticket_number', ''),
'freight_rate': '',
'fields': '',
'bol_contract_number': bol_mapped_data.get('bol_contract_number', ''),
'bol_driver_name': bol_mapped_data.get('bol_driver_name', ''),
'bol_truck_number': bol_mapped_data.get('bol_truck_number', ''),
'outbound_fds_load_number': outbound_mapped_data.get('outbound_fds_load_number', '')
}

# Delete the Image Directory
shutil.rmtree(image_path)

# Delete the Cropped Image Directory
shutil.rmtree(cropped_image_dir)
return Response({"message": "Data Extracted Successfully!!", "context": context}, status=status.HTTP_200_OK)

except ClientError as e:
return Response({"message": f"AWS Textract error: {e}"},
status=status.HTTP_500_INTERNAL_SERVER_ERROR)
else:
return Response({"message": "Failed to fetch the image"}, status=response.status_code)
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.