Skip to content

Commit

Permalink
Trying to fix issue where convert_from_path function cannot find file
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bommu committed Nov 19, 2023
1 parent 8c53706 commit 1679fb7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
21 changes: 7 additions & 14 deletions ACMAS/app/ACMAS_Web/ocr_files/ocr.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
'''
import os
from pdf2image import convert_from_path
import pytesseract
def ocr_driver(pdf_name):
'''if ending_type(pdf_name) == 'pdf':
if ending_type(pdf_name) == 'pdf':
images = 'Hi'
else:
images = pdf_name
ocr_output = images'''
ocr_output = images
return pdf_name

'''

import os
from pdf2image import convert_from_path
import pytesseract


absolute_path = os.path.dirname(__file__)
relative_path = "../ocr_files/lib"
full_path = os.path.join(absolute_path, relative_path)
#absolute_path = os.path.dirname(__file__)
#relative_path = "../ocr_files/"
#full_path = os.path.join(absolute_path, relative_path)


# Gets the ending types of files
Expand All @@ -29,8 +28,6 @@ def ending_type(string):
return string[index + 1:]


def png_conversion(pdf_name):
# makes sure we are only converting pdfs if
# the file type isnt pdf returns ending type
Expand All @@ -40,7 +37,6 @@ def png_conversion(pdf_name):

print("CONVERTING PNG")

if not os.path.isdir('mediafiles/ocr_images'):
os.system("mkdir mediafiles/ocr_images")

Expand All @@ -55,7 +51,6 @@ def png_conversion(pdf_name):



def run_ocr(image_name):
acceptable_formats = set({'png', 'jpg', 'gif', 'tiff'})
if ending_type(image_name) not in acceptable_formats:
Expand All @@ -65,12 +60,10 @@ def run_ocr(image_name):



def ocr_driver(pdf_name):
if ending_type(pdf_name) == 'pdf':
images = png_conversion(pdf_name)
else:
images = pdf_name
ocr_output = run_ocr(images)
return ocr_output
'''
12 changes: 7 additions & 5 deletions ACMAS/app/ACMAS_Web/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def uploadFile(self, uni, course, fType, file):

# Adding file to database
db_file = UploadedFile(
filename=savedFile,
file_dir=file_url,
filename=fileName,
file_dir="/media/" + fileName,
course=Course.objects.get(name=course),
date_uploaded=date.today(),
flag=fType,
Expand All @@ -77,12 +77,14 @@ def uploadFile(self, uni, course, fType, file):
stripped = fileName.split(sep, 1)[0]
fileName2 = stripped + ".txt"
fileText2 = (
f"{ocr.ocr_driver(fileName2)}"
f"{ocr.ocr_driver(fileName)}"
)

# Adding file to filesystem
fs2 = FileSystemStorage()
savedFile2 = fs.save(fileName2, file) # Retrieve the filename
fs = FileSystemStorage()
fileContent2 = ContentFile(fileText2) # Set the content of the new file

savedFile2 = fs.save(fileName2, fileContent2) # Retrieve the filename
file_url2 = fs.url(savedFile2) # Retrieve the file path
print(f'FILE "{savedFile2}" uploaded to "{file_url2}"\n')

Expand Down

0 comments on commit 1679fb7

Please sign in to comment.