diff --git a/prediction_service/__pycache__/views.cpython-310.pyc b/prediction_service/__pycache__/views.cpython-310.pyc index 2e755bf083b35911ef1295762512091c0b587d84..e9aa17e3345c700c626306adbe7ced8b8388f99c 100644 Binary files a/prediction_service/__pycache__/views.cpython-310.pyc and b/prediction_service/__pycache__/views.cpython-310.pyc differ diff --git a/prediction_service/templates/prediction_service/model.html b/prediction_service/templates/prediction_service/model.html index 9303fdad3d3358b8bf0697cbc4a54907a5e71ecf..1fc09a5fbb5ec1962c2b01625e312291179225b8 100644 --- a/prediction_service/templates/prediction_service/model.html +++ b/prediction_service/templates/prediction_service/model.html @@ -10,6 +10,14 @@ <img src="data:image/png;base64,{{ img_str }}" alt="PIL Image"> <h2>Model Layers: {{ layer }}</h2> + + + <form action="/mlmodel/{{ modelID }}/" method="post" enctype="multipart/form-data"> + + {% csrf_token %} + <input type="file" name="photo" id="photoInput" accept="image/*"> + <button type="submit">Upload Photo</button> + </form> </div> diff --git a/prediction_service/views.py b/prediction_service/views.py index 31cb174b27b3c1fa702f5eb3f332c83868c5f0a2..62349c948b55986e5e88cac19b0e9f81491b1121 100644 --- a/prediction_service/views.py +++ b/prediction_service/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render, redirect - +from django.http import HttpResponse from django.contrib import messages from django.contrib.auth.decorators import login_required # messages.debug @@ -119,51 +119,62 @@ import base64 @login_required def mlmodel(request, pk): - matrix = np.asarray([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,67,232,39,0,0], - [0,0,0,0,62,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,120,180,39,0,0], - [0,0,0,0,126,163,0,0,0,0,0,0,0,0,0,0,0,0,0,2,153,210,40,0,0], - [0,0,0,0,220,163,0,0,0,0,0,0,0,0,0,0,0,0,0,27,254,162,0,0,0], - [0,0,0,0,222,163,0,0,0,0,0,0,0,0,0,0,0,0,0,183,254,125,0,0,0], - [0,0,0,0,46,245,0,0,0,0,0,0,0,0,0,0,0,0,0,198,254,56,0,0,0], - [0,0,0,0,120,254,0,0,0,0,0,0,0,0,0,0,0,0,23,231,254,29,0,0,0], - [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,0,0,0,163,254,216,16,0,0,0], - [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,14,86,178,248,254,91,0,0,0,0], - [0,0,0,0,159,254,35,0,0,47,49,116,144,150,241,243,234,179,241,252,40,0,0,0,0], - [0,0,0,0,150,253,237,207,207,207,253,254,250,240,198,143,91,28,233,250,0,0,0,0,0], - [0,0,0,0,119,177,177,177,177,177,98,56,0,0,0,0,0,102,254,220,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,137,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,94,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,96,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,153,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,153,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,96,254,153,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], - [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]) - - # Matrix dimensions - raw_height = matrix.shape[0] - raw_width = matrix.shape[1] - - fig, ax = plt.subplots(1, 1, figsize=(4, 4)) - ax.imshow(matrix, cmap='Greys_r') - - buf = io.BytesIO() - fig.savefig(buf, format='png') - buf.seek(0) - img = Image.open(buf) - - # Convert PIL image to base64 string - buffered = io.BytesIO() - img.save(buffered, format='PNG') - img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') - - mlmodel = MLModel.objects.get(id=pk) - layercount = str(mlmodel.layers) - - return render(request, "prediction_service/model.html", {'img_str': img_str, - 'layer': layercount,}) \ No newline at end of file + if request.method == "POST": + # Handle form submission here + # For example, you can process the uploaded file + uploaded_file = request.FILES['photo'] + # Process the uploaded file as needed + + # Return a response indicating successful upload + return HttpResponse("<h1> You uploaded your photo ! </h1>") + else: + + matrix = np.asarray([[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,67,232,39,0,0], + [0,0,0,0,62,81,0,0,0,0,0,0,0,0,0,0,0,0,0,0,120,180,39,0,0], + [0,0,0,0,126,163,0,0,0,0,0,0,0,0,0,0,0,0,0,2,153,210,40,0,0], + [0,0,0,0,220,163,0,0,0,0,0,0,0,0,0,0,0,0,0,27,254,162,0,0,0], + [0,0,0,0,222,163,0,0,0,0,0,0,0,0,0,0,0,0,0,183,254,125,0,0,0], + [0,0,0,0,46,245,0,0,0,0,0,0,0,0,0,0,0,0,0,198,254,56,0,0,0], + [0,0,0,0,120,254,0,0,0,0,0,0,0,0,0,0,0,0,23,231,254,29,0,0,0], + [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,0,0,0,163,254,216,16,0,0,0], + [0,0,0,0,159,254,0,0,0,0,0,0,0,0,0,14,86,178,248,254,91,0,0,0,0], + [0,0,0,0,159,254,35,0,0,47,49,116,144,150,241,243,234,179,241,252,40,0,0,0,0], + [0,0,0,0,150,253,237,207,207,207,253,254,250,240,198,143,91,28,233,250,0,0,0,0,0], + [0,0,0,0,119,177,177,177,177,177,98,56,0,0,0,0,0,102,254,220,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,137,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,57,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,94,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,96,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,254,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,169,255,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,96,254,153,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], + [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]) + + # Matrix dimensions + raw_height = matrix.shape[0] + raw_width = matrix.shape[1] + + fig, ax = plt.subplots(1, 1, figsize=(4, 4)) + ax.imshow(matrix, cmap='Greys_r') + + buf = io.BytesIO() + fig.savefig(buf, format='png') + buf.seek(0) + img = Image.open(buf) + + # Convert PIL image to base64 string + buffered = io.BytesIO() + img.save(buffered, format='PNG') + img_str = base64.b64encode(buffered.getvalue()).decode('utf-8') + + mlmodel = MLModel.objects.get(id=pk) + layercount = str(mlmodel.layers) + + return render(request, "prediction_service/model.html", {'img_str': img_str, + 'layer': layercount, + 'modelID': pk}) \ No newline at end of file