ソースを参照

showing isos works, working on linking them

bscheibel 4 年 前
コミット
2a37160dea
共有4 個のファイルを変更した129 個の追加10 個の削除を含む
  1. 5 2
      app/static/css/style.css
  2. 10 1
      app/templates/show_image.html
  3. 15 7
      app/views.py
  4. 99 0
      order_bounding_boxes_in_each_block.py

+ 5 - 2
app/static/css/style.css

@@ -54,9 +54,12 @@ body {
   text-align: center;
   margin: 10;
 }
-
+.thick-border {
+        border: 3px solid black;
+        border-collapse: collapse;
+}
 /* Style the image inside the centered container, if needed */
 .centered img {
   max-width: 140%;
   /*border-radius: 50%;
-}
+}

+ 10 - 1
app/templates/show_image.html

@@ -23,7 +23,7 @@
   <div class="top">
     <div style="text-indent:50px;">
       <h1>Extracted Tolerances</h1>
-      <p>Maße: </p>
+      <p>Maße: {{ dims }} </p>
       <p><a href="http://localhost:5000/"> Upload another drawing </a></p>
     </div>
   </div>
@@ -32,6 +32,15 @@
   <div class="bottom">
     <div style="text-indent:50px;">
       <h1>Additional Requirements:</h1>
+      <table>
+
+        {% for k in isos %}
+                <tr>
+                        <td><a href="path/doc.pdf">My Resume</a> {{ k }}</td>
+                </tr>
+        {% endfor %}
+      </table>
+
     </div>
     </div>
 </div>

+ 15 - 7
app/views.py

@@ -3,7 +3,10 @@ import os
 from flask import make_response
 from flask import Flask, request, redirect, url_for, send_from_directory, render_template
 import subprocess
-import time
+import order_bounding_boxes_in_each_block
+import redis
+import random
+import json
 #https://medium.com/@emerico/convert-pdf-to-image-using-python-flask-2864fb655e01
 
 
@@ -27,7 +30,9 @@ def upload_file():
             #image = wandImage(filename=filename)
             #image.save(os.path).join(app.config["UPLOAD_FOLDER"], filename)
             file.save(os.path.join(app.config["UPLOAD_FOLDER"], filename))
-            return redirect(url_for('uploaded_file', filename=filename))
+            uuid = random.randint(100,10000000)
+            order_bounding_boxes_in_each_block.main(uuid, UPLOAD_FOLDER+"/"+filename)
+            return redirect(url_for('uploaded_file', filename=filename, uuid=uuid))
     return '''
     <!doctype html>
     <title>Upload new File</title>
@@ -42,13 +47,15 @@ def convert_pdf(filename):
     subprocess.call(['pdftoppm', '-jpeg', '-singlefile',
                      PDFFILE, '/home/bscheibel/uploads_app/out'])
 
-@app.route('/show/<filename>')
-
-def uploaded_file(filename):
+@app.route('/show/<filename>&<uuid>')
+def uploaded_file(filename, uuid):
     file_out = "out.jpg"
     if filename.endswith(".pdf") or filename.endswith(".PDF"):
         convert_pdf(filename)
-        return render_template('show_image.html', filename=file_out)
+        db = redis.Redis("localhost")
+        #uuid = "dd"
+        isos = json.loads(db.get(uuid))
+        return render_template('show_image.html', filename=file_out, isos=isos)
 
     else:
         filename = filename
@@ -65,4 +72,5 @@ def add_header(response):
     response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
     response.headers['Pragma'] = 'no-cache'
     response.headers['Expires'] = '-1'
-    return response
+    return response
+

+ 99 - 0
order_bounding_boxes_in_each_block.py

@@ -0,0 +1,99 @@
+### FIRST READ EACH BLOCK IN AN ARRAY
+
+from bs4 import BeautifulSoup
+import subprocess
+import redis
+import re
+import json
+
+def get_bound_box(uuid, file):
+    print(file)
+    response = open(file)
+    html_doc = response.read()
+    response.close()
+    html_file = BeautifulSoup(html_doc, 'html.parser')
+
+    all_elements = []
+    blocks = html_file.findAll('block')
+    for block in blocks:
+        list_elements = []
+        words = block.findAll('word')
+        for word in words:
+            word_list = []
+            word_list.append(word["xmin"])
+            word_list.append(word["ymin"])
+            word_list.append(word["xmax"])
+            word_list.append(word["ymax"])
+            word_list.append(word.string)
+            list_elements.append(word_list)
+        all_elements.append(list_elements)
+
+
+    #### NEXT SORT ELEMENTS IN EACH BLOCK BY THEIR X AND Y COORDINATES
+    #### FIRST TRYING XMIN und YMAX
+    ###FIRST CHECKING IF THE ELEMENTS ARE VERTICAL, IF YES THEN NO SORTING
+    new_all_elements = []
+
+    for element in all_elements:
+        later_bigger = (float(element[-1][0])-(float(element[0][0]))) #check if xmin from first element is bigger than xmin from last element
+        abstand_x = abs(float(element[-1][0])-(float(element[0][2])))
+        abstand_y = abs(float(element[-1][3])-float(element[0][1]))
+        if later_bigger >= -5:
+            #print(abstand_x-abstand_y)
+            new_all_elements.append(element)
+        else:
+            new_element = sorted(element, key=lambda k: [float(k[0])])
+            new_all_elements.append(new_element)
+
+
+    """for element in new_all_elements:
+        for blub in element:
+            #print(blub[4])
+
+        #print("\n")"""
+
+
+    db = redis.Redis("localhost")
+    db.set(uuid, "test")
+    return new_all_elements, uuid
+
+def pdf_to_html(uuid,filepath):
+    subprocess.call(['pdftotext', '-bbox-layout',
+                     filepath, str(uuid)+'out.html'])
+
+def extract_isos(result):
+    reg = r"(ISO\s\d\d\d\d*\W?\d?\W?\d?)|(EN\s\d*)"
+    details_ = []
+    for element in result:
+        new_arr = ""
+        #print(element)
+        for x in element:
+            new_arr += x[4] + " "
+        print(new_arr)
+        if re.search(reg,new_arr):
+            #print(new_arr)
+            found = re.findall(reg, new_arr)
+            for f in found:
+                if len(f[0]) != 0:
+                    details_.append(f[0].replace(")",""))
+                if len(f[1]) != 0:
+                    details_.append(f[1])
+    return details_
+
+def main(uuid, result):
+    pdf_to_html(uuid, result)
+    res, uuid = get_bound_box(uuid, str(uuid)+"out.html")
+    isos = extract_isos(res)
+    isos_j = json.dumps(isos)
+    db = redis.Redis("localhost")
+    print(isos)
+    db.set(uuid, str(isos_j))
+
+"""file = "/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.html"
+res, uuid = get_bound_box("uuu", file)
+isos = extract_isos(res)
+print(isos)
+#pdf_to_html("/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.pdf")
+"""
+
+#main("uud","/home/bscheibel/PycharmProjects/dxf_reader/drawings/5129275_Rev01-GV12.pdf")