Skip to content

Commit

Permalink
patch: resolve indentation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tinashechiraya authored May 6, 2024
1 parent 4f67b7b commit 103d0e6
Showing 1 changed file with 9 additions and 144 deletions.
153 changes: 9 additions & 144 deletions django_project/monitor/observation_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,141 +172,6 @@ def convert_to_int(value, default=0):
except (ValueError, TypeError):
return default


# def process_image_classification(image, observation, classification_results):
# try:
# result = classify_image(image)
# classification_results.put(result)
# except (OSError, Image.DecompressionBombError, Image.UnidentifiedImageError) as e:
# return {'status': 'error', 'message': f'Error recognizing image: {str(e)}'}


# @csrf_exempt
# @login_required
# def upload_pest_image(request):
# """
# This view function handles the upload of pest images, associating them with an observation and a site.

# - Creates an empty site and observation.
# - Associates uploaded images with the observation.
# - Returns the observation ID, site ID, and image IDs for further processing.

# Note:
# - The function saves user uploads before saving the observation to facilitate AI calculations on the images.
# - The returned image IDs can be used for image deletion if the user decides to change their selection.

# Expected Data:
# - 'datainput' object: Contains additional information about the observation.
# - 'observationID': Specifies the ID for the observation.
# - Uploaded files: Images to be associated with the observation.

# :param request: The HTTP request object.
# :return: JsonResponse with status, observation ID, site ID, and pest image IDs.
# """
# if request.method == 'POST':
# try:
# with transaction.atomic():

# site_id = request.POST.get('siteId')
# observation_id = request.POST.get('observationId')
# user = request.user
# site_id = convert_to_int(site_id)
# observation_id = convert_to_int(observation_id)

# try:
# site = Sites.objects.get(gid=site_id)
# except Sites.DoesNotExist:
# max_site_id = Sites.objects.all().aggregate(Max('gid'))[
# 'gid__max']
# new_site_id = max_site_id + 1 if max_site_id is not None else 1

# site_name = request.POST.get('siteName', '')
# river_name = request.POST.get('riverName', '')
# description = request.POST.get('siteDescription', '')
# river_cat = request.POST.get('rivercategory', 'rocky')
# latitude = request.POST.get('latitude', 0)
# longitude = request.POST.get('longitude', 0)

# site = Sites(
# gid=new_site_id,
# the_geom=Point(x=float(longitude), y=float(latitude), srid=4326),
# user=user
# )

# site.site_name = site_name
# site.river_name = river_name
# site.description = description
# site.river_cat = river_cat
# site.user = user
# site.save()

# try:
# observation = Observations.objects.get(
# gid=observation_id, site=site)
# except Observations.DoesNotExist:
# max_observation_id = Observations.objects.all().aggregate(Max('gid'))[
# 'gid__max']
# new_observation_id = max_observation_id + \
# 1 if max_observation_id is not None else 1

# observation = Observations.objects.create(
# gid=new_observation_id,
# site=site,
# user=user,
# comment='',
# obs_date=datetime.now()
# )

# # Save images in the request object
# processes = []
# classification_results = Queue()
# for key, image in request.FILES.items():
# if 'pest_' in key:
# group_id = key.split(':')[1]
# if group_id:
# group = GroupScores.objects.get(id=group_id)
# pest_image = ObservationPestImage.objects.create(
# observation=observation,
# group=group
# )
# try:
# pest_image.image = image
# pest_image.save()

# p = multiprocessing.Process(
# target=process_image_classification, args=(image, observation, classification_results)
# )
# processes.append(p)
# p.start()
# except (OSError, Image.DecompressionBombError, Image.UnidentifiedImageError) as e:
# # Handle image recognition errors
# classification_results.append({'status': 'error', 'message': f'Error recognizing image: {str(e)}'})

# for p in processes:
# p.join()

# results_list = []
# while not classification_results.empty():
# results_list.append(classification_results.get())


# return JsonResponse(
# {
# 'status': 'success',
# 'observation_id': observation.gid,
# 'site_id': site.gid,
# 'pest_image_id': pest_image.id,
# 'classification_results': results_list
# }
# )
# except ValidationError as ve:
# return JsonResponse({'status': 'error', 'message': str(ve)})
# except Exception as e:
# # Handle other exceptions
# return JsonResponse({'status': 'error', 'message': str(e)})

# return JsonResponse({'status': 'error', 'message': 'Invalid request method'})

@csrf_exempt
def upload_pest_image(request):
"""
Expand Down Expand Up @@ -411,10 +276,10 @@ def upload_pest_image(request):
# Open the image for classification
result = classify_image(image)
if 'error' not in result:
# Save classification results to the ObservationPestImage instance
pest_image.ml_prediction = result['class']
pest_image.ml_score = result['confidence']
pest_image.save()
# Save classification results to the ObservationPestImage instance
pest_image.ml_prediction = result['class']
pest_image.ml_score = result['confidence']
pest_image.save()
classification_results.append(result)
except (OSError, Image.DecompressionBombError, Image.UnidentifiedImageError) as e:
# Handle image recognition errors
Expand Down Expand Up @@ -561,7 +426,7 @@ def create_observations(request):
if Sites.objects.filter(site_name=site_name).exists():
saveToSite = request.POST.get('saveToSite', 'false').lower()
if saveToSite == 'true':
site = Sites.objects.get(site_name=site_name)
site = Sites.objects.get(site_name=site_name)
else:
return JsonResponse({'status': 'error', 'message': 'Site name already exists'})
else:
Expand Down Expand Up @@ -682,10 +547,10 @@ def build_recent_observations(self, queryset):
user_profile = None

try:
user_obj = User.objects.get(id=observation['user'])
first_name = user_obj.first_name
except User.DoesNotExist:
first_name = None
user_obj = User.objects.get(id=observation['user'])
first_name = user_obj.first_name
except User.DoesNotExist:
first_name = None

recent_observations.append({
'observation': observation['gid'],
Expand Down

0 comments on commit 103d0e6

Please sign in to comment.