def cody_recommend_shoes(request, id): usershoes = UserTops.objects.get(id=id) bit_vector = usershoes.vector vector = np.frombuffer(bit_vector, dtype=np.float32).reshape(1, 64) vector_for_recommend = np.append(vector, np.float32(2)).reshape(1, 65) ib_cody_recommend_ms = Message(settings.ITEMBASE_CODY_RECOMMENDER_HOST, settings.ITEMBASE_CODY_RECOMMENDER_PORT) recommends = ib_cody_recommend_ms.recommand( vector_for_recommend.tostring()) items = Cody.objects.filter(id__in=recommends) cody_serializer = CodySerializer(items, many=True) return Response(cody_serializer.data)
def recognition(request): user = request.user form = TopUploadFileForm(request.POST, request.FILES) if form.is_valid(): top_obj = form.save() img_dir = str(top_obj.img) # img_dir = settings.MEDIA_ROOT + "/" + str(top_obj.img) # change image file format if img_dir[-3:] != "jpg": tmp_img = Image.open(img_dir).convert("RGB") img_extension = img_dir[-3:] img_dir = img_dir[:-3] + "jpg" tmp_img.save(img_dir) os.remove(img_dir[:-3] + img_extension) top_obj.img = str(top_obj.img)[:-3] + "jpg" vector_ms = Message(settings.TOP_VECTORIZATION_HOST, settings.TOP_VECTORIZATION_PORT) bit_vector = vector_ms.imgToBit(img_dir) vector_for_recommend = vector_ms.bitToVector(bit_vector) vector_for_recommend = np.append(vector_for_recommend, np.float32(0)).reshape(1, 65) if bit_vector == b"": return Response(status=status.HTTP_404_NOT_FOUND) else: recommand_ms = Message(settings.RECOGNITION_HOST, settings.RECOGNITION_PORT) recommands = recommand_ms.recommand( vector_for_recommend.tostring()) items = TopsImage.objects.filter(id__in=recommands) topImage_serializer = TopImageSerializer(items, many=True) top_obj.user = user top_obj.vector = bit_vector top_obj.save() userTop_serializer = UserTopsSerializer(top_obj) new_dict = { "userTop_obj": userTop_serializer.data, "similar_things": topImage_serializer.data, } return Response(new_dict) return Response(status=status.HTTP_400_BAD_REQUEST)
def cody_recommend_all(request): user = User.objects.get(username=request.user) userTops_serializer = UserTopsSerializer(user.userTops.all(), many=True) userPants_serializer = UserPantsSerializer(user.userPants.all(), many=True) userShoes_serializer = UserShoesSerializer(user.userShoes.all(), many=True) tops_list = list() for obj in userTops_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") usertops = UserTops.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(usertops.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(usertops.vector, dtype=np.float32).reshape( # 1, 64 # ) tops_list.append(sub_dict) pants_list = list() for obj in userPants_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") userpants = UserPants.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(userpants.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(userpants.vector, dtype=np.float32).reshape( # 1, 64 # ) pants_list.append(sub_dict) shoes_list = list() for obj in userShoes_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") usershoes = UserShoes.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(usershoes.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(usershoes.vector, dtype=np.float32).reshape( # 1, 64 # ) shoes_list.append(sub_dict) user_dict = { "userTops": tops_list, "userPants": shoes_list, "userShoes": shoes_list, } try: cody_recommend_ms = Message(settings.CODY_RECOMMENDER_HOST, settings.CODY_RECOMMENDER_PORT) recommend_cody = cody_recommend_ms.recommend_cody( json.dumps(user_dict).encode("utf-8")) recommend_cody_detail = [] for cody in recommend_cody: tmp = dict() cody_obj = Cody.objects.get(id=cody[0]) tmp["id"] = cody_obj.id tmp["img"] = cody_obj.img.url tmp["jjim"] = cody_obj.jjim tmp["tops"] = UserTops.objects.get(id=cody[1]).img.url tmp["pants"] = UserPants.objects.get(id=cody[2]).img.url tmp["shoes"] = UserShoes.objects.get(id=cody[3]).img.url recommend_cody_detail.append(tmp) return JsonResponse(recommend_cody_detail, safe=False, json_dumps_params={"ensure_ascii": False}) except: return Response(status=status.HTTP_404_NOT_FOUND) return Response(status=status.HTTP_400_BAD_REQUEST)
def recommend_item(request): user = User.objects.get(username=request.user) userTops_serializer = UserTopsSerializer(user.userTops.all(), many=True) userPants_serializer = UserPantsSerializer(user.userPants.all(), many=True) userShoes_serializer = UserShoesSerializer(user.userShoes.all(), many=True) tops_list = list() for obj in userTops_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") usertops = UserTops.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(usertops.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(usertops.vector, dtype=np.float32).reshape( # 1, 64 # ) tops_list.append(sub_dict) pants_list = list() for obj in userPants_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") userpants = UserPants.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(userpants.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(userpants.vector, dtype=np.float32).reshape( # 1, 64 # ) pants_list.append(sub_dict) shoes_list = list() for obj in userShoes_serializer.data: sub_dict = dict() sub_dict["id"] = obj.get("id") usershoes = UserShoes.objects.get(id=sub_dict["id"]) sub_dict["vector"] = base64.b64encode(usershoes.vector).decode("ascii") # sub_dict["vector"] = np.frombuffer(usershoes.vector, dtype=np.float32).reshape( # 1, 64 # ) shoes_list.append(sub_dict) user_dict = { "userTops": tops_list, "userPants": shoes_list, "userShoes": shoes_list, } item_recommend_ms = Message(settings.ITEM_RECOMMENDER_HOST, settings.ITEM_RECOMMENDER_PORT) recommend_items = item_recommend_ms.recommend_item( json.dumps(user_dict).encode("utf-8")) recommend_item_infos = [] for item in recommend_items: tmp = dict() obj = None if item[0][3] == "1": tmp["img"] = TopsImage.objects.get(id=item[0]) obj = Tops.objects.get(id=tmp["img"].top_id) elif item[0][3] == "2": tmp["img"] = PantsImage.objects.get(id=item[0]) obj = Pants.objects.get(id=tmp["img"].pants_id) elif item[0][3] == "3": tmp["img"] = ShoesImage.objects.get(id=item[0]) obj = Shoes.objects.get(id=tmp["img"].shoes_id) tmp["img"] = tmp["img"].img.url tmp["id"] = obj.id tmp["brand"] = obj.brand tmp["product"] = obj.product tmp["category"] = obj.category tmp["shop"] = obj.shop tmp["item_url"] = obj.item_url tmp["item_cody"] = [] for cody_id in item[1]: cody = Cody.objects.get(id=int(cody_id)) tmp["item_cody"].append(cody.img.url) recommend_item_infos.append(tmp) return JsonResponse(recommend_item_infos, safe=False, json_dumps_params={"ensure_ascii": False})