[ad_1]
I’m research React Native and Django Improvement and I create an app which acknowledge a textual content from picture. I spotted POST technique from React Native to Django Server however I do not perceive ship the results of recognition again to React.
How I can resolve this downside?
Django Server:
views.py:
from .serializers import PostSerializer
from .fashions import Ocr
from rest_framework.views import APIView
from rest_framework.parsers import MultiPartParser, FormParser
from rest_framework.response import Response
from rest_framework import standing
# Create your views right here.
from django.http.response import JsonResponse
# Create your views right here.
# import pytesseract to transform textual content in picture to string
import pytesseract
# import summarize to summarize the ocred textual content
from .types import ImageUpload
import os
# import Picture from PIL to learn picture
from PIL import Picture
from django.conf import settings
# Create your views right here.
class PostView(APIView):
parser_classes = (MultiPartParser, FormParser)
def get(self, request, *args, **kwargs):
posts = Ocr.objects.all()
serializer = PostSerializer(posts, many=True)
print(serializer.information)
return Response(serializer.information)
def publish(self, request, *args, **kwargs):
posts_serializer = PostSerializer(information=request.information)
if posts_serializer.is_valid():
textual content = ""
message = ""
posts_serializer.save()
strive:
posts_serializer.save()
picture = request.FILES['image']
picture = picture.identify
path = settings.MEDIA_ROOT
pathz = path + "/photographs/" + picture
textual content = pytesseract.image_to_string(Picture.open(pathz), lang='rus+eng')
os.take away(pathz)
besides Exception:
message = "verify your filename and guarantee it would not have any area or verify if it has any textual content"
context = {
'textual content': textual content,
'message': message
}
print(context)
return Response(posts_serializer.information, standing=standing.HTTP_201_CREATED)
else:
return Response(posts_serializer.errors, standing=standing.HTTP_400_BAD_REQUEST)
React-Native:
CameraScreen.js:
const postToServer = (img) => {
const formData = new FormData();
formData.append('picture', {
uri: img.uri,
kind: 'picture/jpeg',
identify: 'check.jpg',
})
formData.append('title', 'title');
fetch('http://192.168.0.149:8000/api/textocr/', {
technique: 'POST',
physique: formData,
// If you happen to add this, add will not work
headers: {
'Settle for': 'utility/json',
'Content material-Sort': 'multipart/form-data',
}
});
}
[ad_2]
