abhicodes commited on
Commit
0b8812e
1 Parent(s): 4a1f0e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -7
app.py CHANGED
@@ -1,12 +1,35 @@
1
  import cv2
2
  import easyocr
3
  import gradio as gr
4
- import base64
 
 
 
 
5
 
6
  # Instance text detector
7
  reader = easyocr.Reader(['en'], gpu=False)
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def text_extraction(image):
 
 
 
 
 
 
10
  text_ = reader.readtext(image)
11
 
12
  threshold = 0.25
@@ -14,19 +37,19 @@ def text_extraction(image):
14
  for t_, t in enumerate(text_):
15
  bbox, text, score = t
16
 
17
- if score > threshold:
18
- cv2.rectangle(image, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (255, 0, 0), 2)
19
 
20
- retval, buffer = cv2.imencode('.jpg', image)
21
- img_base64 = base64.b64encode(buffer).decode('utf-8')
22
 
23
- return img_base64
 
24
 
25
  # Define Gradio interface
26
  iface = gr.Interface(
27
  fn=text_extraction,
28
  inputs=gr.Image(),
29
- outputs=["image"]
30
  )
31
 
32
  # Launch the Gradio interface
 
1
  import cv2
2
  import easyocr
3
  import gradio as gr
4
+ import numpy as np
5
+ import requests
6
+
7
+ API_URL = "https://api-inference.huggingface.co/models/dima806/facial_emotions_image_detection"
8
+ headers = {"Authorization": "Bearer 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"}
9
 
10
  # Instance text detector
11
  reader = easyocr.Reader(['en'], gpu=False)
12
 
13
+
14
+ def query(image):
15
+ image_data = np.array(image, dtype=np.uint8)
16
+
17
+ # Convert the image data to binary format (JPEG)
18
+ _, buffer = cv2.imencode('.jpg', image_data)
19
+
20
+ # Convert the binary data to bytes
21
+ binary_data = buffer.tobytes()
22
+
23
+ response = requests.post(API_URL, headers=headers, data=binary_data)
24
+ return response.json()
25
+
26
  def text_extraction(image):
27
+
28
+ # Facial Expression Detection
29
+ global text_content
30
+ text_content = ''
31
+ facial_data = query(image)
32
+
33
  text_ = reader.readtext(image)
34
 
35
  threshold = 0.25
 
37
  for t_, t in enumerate(text_):
38
  bbox, text, score = t
39
 
40
+ text_content = text_content + ' ' + ' '.join(text)
 
41
 
42
+ if score > threshold:
43
+ cv2.rectangle(image, tuple(map(int, bbox[0])), tuple(map(int, bbox[2])), (0, 255, 0), 5)
44
 
45
+ #output the image
46
+ return image, text_content, facial_data
47
 
48
  # Define Gradio interface
49
  iface = gr.Interface(
50
  fn=text_extraction,
51
  inputs=gr.Image(),
52
+ outputs=[gr.Image(), gr.Textbox(label="Text Content"), gr.JSON(label="Facial Data")]
53
  )
54
 
55
  # Launch the Gradio interface