Skip to content

Commit

Permalink
Fix ngrok bug on Colab
Browse files Browse the repository at this point in the history
  • Loading branch information
lannguyen0910 committed May 1, 2024
1 parent 57d95d8 commit 2bc9b90
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ food-detection-yolov5


<details open> <summary><strong>Dev logs</strong></summary>
<strong><i>[01/05/2024]</i></strong> Fix ngrok bug on Colab (migrate to pyngrok)<br>
<strong><i>[24/10/2023]</i></strong> Clean and refactor repo. Integrate YOLOv8 to food detection.<br>
<strong><i>[07/03/2022]</i></strong> Big refactor. Integrate object detection, image classification, semantic segmentation into one <b><i>Ship of Theseus</i></b>.<br>
<strong><i>[31/01/2022]</i></strong> Update to new YOLOv5 latest versions P5-P6. Can load checkpoints from original repo.<br>
Expand Down
67 changes: 29 additions & 38 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,48 @@
import os
import threading
import argparse
import getpass

from flask import Flask
from flask_cors import CORS
from flask_ngrok import run_with_ngrok
from flask import Flask
from pyngrok import ngrok, conf

from backend.routes import set_routes
from backend.constants import UPLOAD_FOLDER, CSV_FOLDER, DETECTION_FOLDER, SEGMENTATION_FOLDER, METADATA_FOLDER

parser = argparse.ArgumentParser('Online Food Recognition')
parser.add_argument('--ngrok', action='store_true',
default=False, help="Run on local or ngrok")
parser.add_argument('--host', type=str,
default='localhost:8000', help="Local IP")
parser.add_argument('--host', type=str, default='localhost', help="Local IP")
parser.add_argument('--port', type=int, default=5000, help="Local port")
parser.add_argument('--debug', action='store_true',
default=False, help="Run app in debug mode")


app = Flask(__name__, template_folder='templates', static_folder='static')
CORS(app, resources={r"/api/*": {"origins": "*"}})

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 1
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['CSV_FOLDER'] = CSV_FOLDER
app.config['DETECTION_FOLDER'] = DETECTION_FOLDER
app.config['SEGMENTATION_FOLDER'] = SEGMENTATION_FOLDER

set_routes(app)
args = parser.parse_args()

if __name__ == '__main__':
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
if not os.path.exists(DETECTION_FOLDER):
os.makedirs(DETECTION_FOLDER, exist_ok=True)
if not os.path.exists(SEGMENTATION_FOLDER):
os.makedirs(SEGMENTATION_FOLDER, exist_ok=True)
if not os.path.exists(CSV_FOLDER):
os.makedirs(CSV_FOLDER, exist_ok=True)
if not os.path.exists(METADATA_FOLDER):
os.makedirs(METADATA_FOLDER, exist_ok=True)

args = parser.parse_args()
app = Flask(__name__, template_folder='templates', static_folder='static')

CORS(app, resources={r"/api/*": {"origins": "*"}})
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['CSV_FOLDER'] = CSV_FOLDER
app.config['DETECTION_FOLDER'] = DETECTION_FOLDER
app.config['SEGMENTATION_FOLDER'] = SEGMENTATION_FOLDER

if args.ngrok:
run_with_ngrok(app)
app.run()
print("Enter your authtoken, which can be copied from https://dashboard.ngrok.com/get-started/your-authtoken")
conf.get_default().auth_token = getpass.getpass()
public_url = ngrok.connect(args.port).public_url
print(
f" * ngrok tunnel \"{public_url}\" -> \"http://127.0.0.1:{args.port}/\"")
app.config['BASE_URL'] = public_url
else:
hostname = str.split(args.host, ':')
if len(hostname) == 1:
port = 4000
else:
port = hostname[1]
host = hostname[0]

app.run(host=host, port=port, debug=args.debug, use_reloader=False,
ssl_context='adhoc')
app.config['BASE_URL'] = f"http://{args.host}:{args.port}"

set_routes(app)

for folder in [UPLOAD_FOLDER, DETECTION_FOLDER, SEGMENTATION_FOLDER, CSV_FOLDER, METADATA_FOLDER]:
os.makedirs(folder, exist_ok=True)

threading.Thread(target=app.run, kwargs={
"host": args.host, "port": args.port, "debug": args.debug, "use_reloader": False}).start()
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ omegaconf
pycocotools
gdown>=4.4.0
flask-cors
flask_ngrok
pyngrok
cryptography
tabulate
segmentation-models-pytorch
Expand Down
6 changes: 3 additions & 3 deletions theseus/utilities/visualization/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def draw_mask(polygons, mask_img):

def draw_polylines(image, polygons):
image = cv2.polylines(
image, [np.array(polygons, dtype=np.int)], True, (0, 0, 1), 2)
image, [np.array(polygons, dtype=int)], True, (0, 0, 1), 2)
return image


Expand Down Expand Up @@ -136,15 +136,15 @@ def plot_one_box(img, box, key=None, value=None, color=None, line_thickness=None
thickness=tf, lineType=cv2.FONT_HERSHEY_SIMPLEX)

# boxes input is xywh
boxes = np.array(boxes, np.int)
boxes = np.array(boxes, int)
img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)

for idx, (box, label_id, score) in enumerate(zip(boxes, label_ids, scores)):
if label_names is not None:
label = label_names[idx]
if obj_list is not None:
label = obj_list[label_id]

new_color = tuple(i*255.0 for i in color_list[int(label_id)])

plot_one_box(
Expand Down
2 changes: 1 addition & 1 deletion theseus/utilities/visualization/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def draw_polygon_ocr(self, polygons, texts=None, font='assets/fonts/aachenb.ttf'

# Reduce opacity of original image
o_image = reduce_opacity(image)
i_masked = (np.bitwise_not(mask)/255).astype(np.int)
i_masked = (np.bitwise_not(mask)/255).astype(int)
o_image = o_image * i_masked

# Add two image
Expand Down

0 comments on commit 2bc9b90

Please sign in to comment.