コード例 #1
0
def crtdetail(request, video):
    from PIL import Image
    import cv2
    video_instance = Video.objects.get(pk=video)

    video_url = video_instance.url
    use_i_th_frame = 300  #30==1sec

    data = PieChart.objects.filter(video_id=video_url)
    if data.count() < 1:
        print("There are no data. start task")
        current_path = os.path.dirname(os.path.abspath(__file__))
        code_path = os.path.abspath(os.path.join(current_path, 'code'))
        videoModule_path = os.path.abspath(
            os.path.join(code_path, 'VideoModule'))
        sys.path.append(videoModule_path)
        from Commander import Commander

        commander = Commander()
        dumped, max_emotion_list, i_list, face_list = commander.for_youtube_video_TimeLine(
            use_i_th_frame, video_url)
        will_inserted = PieChart(video_id=str(video), json_data=dumped)

        img_path_list = []
        for j in range(len(i_list)):
            fileName = str(video) + "_" + str(i_list[j]) + ".png"
            save_dir = os.path.abspath(os.path.join(current_path, 'images'))
            save_dir = os.path.abspath(os.path.join(save_dir, fileName))
            destRGB = cv2.cvtColor(face_list[j], cv2.COLOR_BGR2RGB)
            pic_file = Image.fromarray(destRGB, 'RGB')
            pic_file.save(save_dir)
            img_path_list.append('./images/' + fileName)

        timeLog_list = []
        for j in range(len(i_list)):
            second = (i_list[j] // 30) % 60
            minute = ((i_list[j] // 30) // 60) % 60
            hour = ((i_list[j] // 30) // 60) // 60

            time_str = ""
            if hour < 10:
                time_str += ('0' + str(hour))
            else:
                time_str += str(hour)
            time_str += ":"
            if minute < 10:
                time_str += ('0' + str(minute))
            else:
                time_str += str(minute)
            time_str += ":"
            if second < 10:
                time_str += ('0' + str(second))
            else:
                time_str += str(second)
            timeLog_list.append(
                TimeLog(top_sentiment=max_emotion_list[j],
                        url=video_url,
                        time=time_str,
                        img_path=img_path_list[j]))
        for j in range(len(i_list)):
            timeLog_list[j].save()
        will_inserted.save()

    else:  #already exist
        print("data already exist")
        pass

    return render(request, "yougam/index.html")
コード例 #2
0
def userdetail(request, video):

   video_url = Video.objects.get(pk=video)
   temp_url = video_url.url
   iframe_url = temp_url.replace('https://www.youtube.com/watch?v=','https://www.youtube.com/embed/')

   if Comment.objects.filter(video=video_url.id).count() < 1:
      comment_module_path=os.path.join(os.path.dirname( os.path.abspath( __file__ ) ), 'code/crawler')
      sys.path.append(comment_module_path)

      module_path=os.path.join(os.path.dirname(os.path.abspath( __file__ ) ), 'code/predict_sentiment')
      sys.path.append(module_path)

      comment_module_path2=os.path.join(os.path.dirname( os.path.abspath( __file__ ) ), 'code/predict_sentiment6')
      sys.path.append(comment_module_path2)

      with open("yougam/static/api_key/youtube_api.txt", "r") as y_api :
         YOUTUBE_API_KEY = y_api.readline()

      from youtube_api_cmd import YouTubeApi
      import sentiment_count
      import predict
      import spellcheck
      import random

      import sentiment_wordcloud

      comment_obj = YouTubeApi(100,video_url.url,YOUTUBE_API_KEY)

      comment_list = comment_obj.get_video_comment()
      video_title  = comment_obj.get_video_title()
      video_url.title = video_title
      video_url.generate()
      print(video_title)
      print("--comment collect complete--")

      comments = spellcheck.spellchecker(comment_list)
      print("--comment spellcheck complete--")

      label = predict.labeling(comments)
      print("--comment positibe/negative complete--")

       #------crawling----------

      predicted_comment_list = sentiment_count.predict_senti6(comment_list)
      predict_replies_list = {}
      reply_idx = 1

      for comment_info in predicted_comment_list:
         comment_text = predicted_comment_list[comment_info]['comment']
         comment_author = predicted_comment_list[comment_info]['author']
         comment_period = predicted_comment_list[comment_info]['period']
         comment_like = predicted_comment_list[comment_info]['like']
         comment_label = predicted_comment_list[comment_info]['label']
         comment_labelpn = label[comment_info]['label_pn']
         parsed_date = dateutil.parser.parse(comment_period)
         parsed_date = parsed_date.strftime('%Y/%m/%d')
         ran = random.randint(1,9)
         parent_comment = video_url.comment_set.create(video = video, cid=comment_info,cmt = comment_text,label= comment_labelpn,label6 = comment_label, author = comment_author, period = parsed_date, like = comment_like,randnum=ran)
         # print(predicted_comment_list[comment_info])

         if 'replies' in predicted_comment_list[comment_info].keys():
            replies_list = predicted_comment_list[comment_info]['replies']
            replies_list2 = label[comment_info]['replies']
            # predicted_replies_list = sentiment_count.predict_senti6(replies_list)
            predicted_replies_list2 = spellcheck.spellchecker(replies_list2)
            predicted_replies_list2 = predict.labeling(predicted_replies_list2)

            parent_id = parent_comment.id
            for reply_info in replies_list:
               predict_replies_list[reply_idx] = replies_list[reply_info]
               reply_text = replies_list[reply_info]["comment"]
               reply_author = replies_list[reply_info]["author"]
               reply_period = replies_list[reply_info]["period"]
               reply_like = replies_list[reply_info]["like"]
               # reply_label = replies_list[reply_info]["label"]
               reply_labelpn = replies_list[reply_info]["label_pn"]
               parsed_date = dateutil.parser.parse(reply_period)
               parsed_date = parsed_date.strftime('%Y/%m/%d')
               this_reply = parent_comment.replydata_set.create(video = video,pid=comment_info, parent_id = parent_id, comment = reply_text, label = reply_labelpn, author = reply_author, period = parsed_date, like = reply_like)
               predict_replies_list[reply_idx]["rid"] = this_reply.id
               reply_idx += 1

      predicted_replies_list = sentiment_count.predict_senti6(predict_replies_list)
      for i in predicted_replies_list:
          find_id = predicted_replies_list[i]["rid"]
          found_reply = ReplyData.objects.get(id=find_id)
          found_reply.label6 = predicted_replies_list[i]["label"]
          found_reply.generate()
      print("--comment6 predict complete--")

      predict_count_cmt = sentiment_count.sentenceCount(predicted_comment_list)
      predict_count_reply = sentiment_count.sentenceCount(predicted_replies_list)
      sentiment_wordcloud.wordcloud(predicted_comment_list, predicted_replies_list, video_url.id)

      cmt_count_list = []
      reply_count_list = []
      count_list = []

      for senti in predict_count_cmt:
         cmt_count_list.append(predict_count_cmt[senti])
      for senti in predict_count_reply:
         reply_count_list.append(predict_count_reply[senti])

      for i in range(0,6):
         count_list.append(cmt_count_list[i] + reply_count_list[i])
         print(count_list[i])
      video_url.sentiment_neutral = count_list[0]
      video_url.sentiment_happy = count_list[1]
      video_url.sentiment_sad = count_list[2]
      video_url.sentiment_surprise = count_list[3]
      video_url.sentiment_anger = count_list[4]
      video_url.sentiment_fear = count_list[5]
      video_url.generate()


   cmt_neutral = Comment.objects.filter(video=video, label6='neutral').count()
   cmt_happy = Comment.objects.filter(video=video, label6='happy').count()
   cmt_sad = Comment.objects.filter(video=video, label6='sad').count()
   cmt_surprise = Comment.objects.filter(video=video, label6='surprise').count()
   cmt_anger = Comment.objects.filter(video=video, label6='anger').count()
   cmt_fear = Comment.objects.filter(video=video, label6='fear').count()

   rep_neutral = Comment.objects.filter(video=video, label6='neutral').count()
   rep_happy = Comment.objects.filter(video=video, label6='happy').count()
   rep_sad = Comment.objects.filter(video=video, label6='sad').count()
   rep_surprise = Comment.objects.filter(video=video, label6='surprise').count()
   rep_anger = Comment.objects.filter(video=video, label6='anger').count()
   rep_fear = Comment.objects.filter(video=video, label6='fear').count()



   loaded_count_list = [0,0,0,0,0,0]
   loaded_count_list[0] = cmt_neutral + rep_neutral
   loaded_count_list[1] = cmt_happy + rep_happy
   loaded_count_list[2] = cmt_sad + rep_sad
   loaded_count_list[3] = cmt_surprise + rep_surprise
   loaded_count_list[4] = cmt_anger + rep_anger
   loaded_count_list[5] = cmt_fear + rep_fear
   print(loaded_count_list)

   comments = Comment.objects.filter(video=video)
   reply = ReplyData.objects.filter(video=video)
   temp_url = video_url.url
   video_title = video_url.title
   iframe_url = temp_url.replace('https://www.youtube.com/watch?v=','https://www.youtube.com/embed/')
   vid = video_url.id
   print("comment predict complete!")
   print("video predict start!")



   from PIL import Image
   import cv2
   video_instance = Video.objects.get(pk=video)
   video_url = video_instance.url
   use_i_th_frame = 300 #30==1sec
   data = PieChart.objects.filter(video_id=video)#video_url) #url로 하고 싶으면 이걸 사용.
   if data.count() < 1:
      print("There are no data. start task")
      current_path = os.path.dirname( os.path.abspath( __file__ ) )
      code_path = os.path.abspath(os.path.join(current_path, 'code'))
      videoModule_path = os.path.abspath(os.path.join(code_path, 'VideoModule'))
      sys.path.append(videoModule_path)
      from Commander import Commander
      commander = Commander()
      dumped, max_emotion_list, i_list, face_list = commander.for_youtube_video_TimeLine(use_i_th_frame, video_url)
      will_inserted = PieChart(video_id = str(video), json_data = dumped)
      img_path_list = []
      for j in range(len(i_list)):
         fileName = str(video) + "_" + str(i_list[j]) + ".png"
         save_dir = os.path.abspath(os.path.join(current_path, os.pardir))
         save_dir = os.path.abspath(os.path.join(save_dir, 'media'))
         save_dir = os.path.abspath(os.path.join(save_dir, fileName))
         destRGB = cv2.cvtColor(face_list[j], cv2.COLOR_BGR2RGB)
         pic_file = Image.fromarray(destRGB, 'RGB')
         pic_file.save(save_dir)
         img_path_list.append( fileName )
      timeLog_list = []
      for j in range(len(i_list)):
         second = (i_list[j]//30)%60
         minute = ((i_list[j]//30)//60 )%60
         hour = ((i_list[j]//30)//60 )//60
         time_str = ""
         if hour<10:
             time_str += ('0' + str(hour))
         else:
             time_str += str(hour)
         time_str += ":"
         if minute<10:
             time_str += ('0' + str(minute))
         else:
             time_str += str(minute)
         time_str += ":"
         if second<10:
             time_str += ('0' + str(second))
         else:
             time_str += str(second)
         timeLog_list.append( TimeLog(top_sentiment = max_emotion_list[j], url = video_url, time = time_str, img_path = img_path_list[j]) )
      for j in range(len(i_list)):
         timeLog_list[j].save()
      will_inserted.save()
   else:
      print("data already exist")
      pass
   video_id = video
   video_instance = Video.objects.get(pk=video_id)
   video_url = video_instance.url
   logs = TimeLog.objects.filter(url=video_url)
   poll_results = PieChart.objects.get(video_id=video_id)

   return render(request, "yougam/user.html", {"count":loaded_count_list,"cmts":comments, "video_id":vid, "iframe_url":iframe_url,"logs": logs, "json" : SafeString(poll_results.json_data), "video_title":video_title,"reply":reply})