4
0
This repository has been archived on 2020-07-25. You can view files and clone it, but cannot push or open issues or pull requests.
classification-service/classification_service/views/classify_view.py

63 lines
1.8 KiB
Python
Raw Normal View History

2020-04-15 00:33:06 +02:00
#!/usr/bin/env python3
2020-04-15 02:03:59 +02:00
import os
2020-04-15 00:33:06 +02:00
from flask import request, jsonify
from flask_classful import FlaskView
from utils import json_required
2020-04-15 02:03:59 +02:00
import requests
import tempfile
from pyAudioAnalysis.audioTrainTest import load_model, load_model_knn, classifier_wrapper
2020-04-15 00:33:06 +02:00
class ClassifyView(FlaskView):
@json_required
def post(self):
2020-04-15 02:03:59 +02:00
request.json
_, temp_model_name = tempfile.mkstemp()
temp_means_name = temp_model_name + "MEANS"
r = requests.get("http://model-service/model/$default/details")
r.raise_for_status()
model_details = r.json()
try:
r = requests.get("http://model-service/model/$default")
r.raise_for_status()
with open(temp_model_name, 'wb') as f:
f.write(r.content)
r = requests.get("http://model-service/model/$default?means")
r.raise_for_status()
with open(temp_means_name, 'wb') as f:
f.write(r.content)
if model_details['type'] == 'knn':
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model_knn(temp_model_name)
else:
classifier, mean, std, classes, mid_window, mid_step, short_window, short_step, compute_beat \
= load_model(temp_model_name)
# class_id, probability = classifier_wrapper(classifier, model_details['type'], feature_vector)
finally: # bruuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuh
try:
os.remove(temp_model_name)
except FileNotFoundError:
pass
try:
os.remove(temp_means_name)
except FileNotFoundError:
pass
return jsonify({})