Issue
I want to upload an image file using an input file tag from a django template. Then i want to do 2 things:
1) First i want to store the uploaded image in a directory in my project. I want to upload the file in img directory under static directory in my app so to access it easily.
2) Second i want to send the filename of this newly stored image to a view function called detect_im()
TEMPLATE:
<form style="display:inline-block;">
<input type="file" class="form-control-file">
<input type="submit" value="Upload"=>
</form>
VIEW FUNCTION in views.py
def detect_im(request):
haar_file = 'C:\\Users\\Aayush\\ev_manage\\face_detector\\haarcascade_frontalface_default.xml'
datasets = 'datasets\\'
myid = random.randint(1111, 9999)
path = "C:\\Users\\Aayush\\ev_manage\\face_detector\\" + datasets + str(myid)
if not os.path.isdir(path):
os.mkdir(path)
(width, height) = (130, 100)
face_cascade = cv2.CascadeClassifier(haar_file)
filename = "" //I WANT THE STORED FILE NAME VALUE HERE TO COMPLETE THE PATH FOR FURTHER DETECTION PROCESS BY OPENCV.
image_path = "C:\\Users\\Aayush\\ev_manage\\face_detector\\static\\img\\" + filename
count = 1
while count < 30:
im = cv2.imread(image_path)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 4)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x + w, y + h), (255, 0, 0), 2)
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (width, height))
cv2.imwrite('% s/% s.png' % (path, count), face_resize)
count += 1
key = cv2.waitKey(10)
if key == 27:
break
return render(request, 'add_dataset.html', {'uid': myid})
The final flow should be like that, user adds the image and click on upload then image gets uploaded to directory and detect_im() function is called with the filename and that filename is used in the path variable for the opencv to detect the face out of it.
Thanks for reading. Please post an answer with at least some code additions because i am a rookie in python.
Solution
upload an image file using an input file tag from a django template and store the uploaded image in a directory in my project
Try to use Pillow:
First, install the package
pip3 install Pillow
Next, create a field and function
in your models.py:
# models.py
import os
from django.db import models
from django.utils.timezone import now as timezone_now
from django.utils.translation import ugettext_lazy as _
def upload_to(instance, filename):
now = timezone_now()
base, ext = os.path.splitext(filename)
ext = ext.lower()
return f"(your_dir)/{now:%Y/%m/%Y%m%d%H%M%S}{ext}"
# Do add the date as it prevents the same file name from occuring twice
# the ext is reserved for the file type and don't remove it
class MyExampleClass(models.Model):
...
picture = models.ImageField(_("Picture"), upload_to=upload_to, blank=True, null=True)
Then, create a form to upload the image:
# forms.py
from django import forms
class MyExampleForm(forms.ModelForm):
class Meta:
model = MyExampleClass
fields = ["picture"]
Remember to render it in a view and your url:
# views.py
from django.shortcuts import render, redirect
from .forms import MyExampleForm
def add_image(request):
form = MyExampleForm()
if request.method == "POST":
form = MyExampleForm(data=request.POST, files=request.FILES)
if form.is_valid():
form.save()
return redirect("")
else:
return render(request, "myexamplehtml.html", {"form": form})
# quotes/urls.py
from django.urls import path
from .views import MyExampleView
urlpatterns = [
path('foobar/', add_image, name='myexampleview'),
]
Finally, add it in your HTML file:
{% block content %}
<form method="post" action="your_url" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">submit</button>
</form>
{% endblock %}
About the second part, I think that you can access your user's variable like this:
MyExampleModel._meta.get_field('picture')
and then put this code into wherever you want it to function
Hope that this works!
Answered By - crimsonpython24
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.