Нашел
такой способ обрезать изображения. Пытаюсь его реализовать таким образом:
def upload_image(request):
data = {"success": False}
if request.method == 'POST':
form = forms.FileUploadForm(data=request.POST, files=request.FILES)
if form.is_valid():
image = grab_image(request.FILES['photo']);
#fs = FileSystemStorage()
#fs.save('d', image)
#path_to_image = settings.MEDIA_ROOT + '\\' + image.name
#uploaded_image = im.upload_image(path_to_image, title=image.name)
#uploadet_link = uploaded_image.link
#fs.delete(image.name)
# convert the image to grayscale, load the face cascade detector,
# and detect faces in the image
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
detector = cv2.CascadeClassifier(FACE_DETECTOR_PATH)
rects = detector.detectMultiScale(image, scaleFactor=1.1, minNeighbors=5,
minSize=(30, 30), flags=cv2.CASCADE_SCALE_IMAGE)
# construct a list of bounding boxes from the detection
# update the data dictionary with the faces detected
data.update({"num_faces": len(rects), "faces": rects, "success": True})
#fs = FileSystemStorage()
#fs.save(uuid.uuid4(), crop_img)
if(len(rects) > 1):
pass
elif(len(rects) < 1):
pass
else:
rect_list = str(rects[0]).split()
for x in range(len(rect_list)):
print(rect_list[x])
crop_img = image[rect_list[1]:rect_list[1]+rect_list[3], rect_list[0]:rect_list[0]+rect_list[2]]#здесь возникает ошибка
#fs = FileSystemStorage()
#fs.save(uuid.uuid4(), crop_img)
crop_image()
else:
print(form.errors)
return JsonResponse(data)
def grab_image(stream):
data = stream.read()
# convert the image to a NumPy array and then read it into
# OpenCV format
image = np.asarray(bytearray(data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
, но получаю ошибку -
TypeError: slice indices must be integers or None or have an __index__ method
. В чем может быть проблема?
P.S.это Django проект, но думаю, что к вопросу это не имеет отношения