opencv 输出:未找到匹配项

bfrts1fy  于 2023-11-22  发布在  其他
关注(0)|答案(1)|浏览(215)

我正在开发一个Android应用程序,它将文本转换为相应的视频,我使用Python和Java。视频存储在Android Studio的assets目录中。模型访问asset目录但不读取数据。这是我的代码

  1. #python
  2. import cv2
  3. import numpy as np
  4. import spacy
  5. import nltk
  6. from nltk.corpus import wordnet
  7. from itertools import chain
  8. import os
  9. import time
  10. AssetManager = Python.getPlatform().getApplication().getAssets()
  11. nltk.download('wordnet')
  12. # Load the SpaCy language model
  13. nlp = spacy.load("en_core_web_sm")
  14. # Function to process text and concatenate videos
  15. def process_text_and_generate_video(input_text, asset_manager, context):
  16. word=''
  17. # Tokenize using spacy
  18. doc = nlp(input_text)
  19. # Initialize a dictionary to map POS tags
  20. pos_dict = {"NOUN": "noun", "VERB": "verb", "ADJ": "adj", "ADV": "adv", "PRON": "pron", "ADP": "adp"}
  21. # Array to store video frames
  22. frames = []
  23. # Iterate through spacy's tokenized results
  24. for token in doc:
  25. word = token.text
  26. pos_tag = token.pos_
  27. if pos_tag in pos_dict:
  28. try:
  29. # Copy the video file from assets to a temporary directory
  30. file_path = f"assets/{word}.mp4"
  31. print(f"Trying to open file: {file_path}")
  32. with asset_manager.open(file_path) as fd:
  33. temp_file_path = os.path.join(context.getCacheDir(), f"{word}.mp4")
  34. with open(temp_file_path, 'wb') as out:
  35. out.write(fd.read())
  36. # Read video frames into a numpy array
  37. cap = cv2.VideoCapture(temp_file_path)
  38. ret, frame = cap.read()
  39. if ret:
  40. frames.append(frame)
  41. except Exception as e:
  42. print(f"Exception occurred: {e}")
  43. else:
  44. # Get synonyms of the word
  45. synonyms = wordnet.synsets(word)
  46. synonyms = set(chain.from_iterable([word.lemma_names() for word in synonyms]))
  47. # Check if any synonym has a corresponding video in the directory
  48. for synonym in synonyms:
  49. try:
  50. # Copy the video file from assets to a temporary directory
  51. file_path = f"assets/{synonym}.mp4"
  52. print(f"Trying to open file: {file_path}")
  53. with asset_manager.open(file_path) as fd:
  54. temp_file_path = os.path.join(context.getCacheDir(), f"{synonym}.mp4")
  55. with open(temp_file_path, 'wb') as out:
  56. out.write(fd.read())
  57. # Read video frames into a numpy array
  58. cap = cv2.VideoCapture(temp_file_path)
  59. ret, frame = cap.read()
  60. if ret:
  61. frames.append(frame)
  62. break
  63. except Exception as e:
  64. print(f"Exception occurred: {e}")
  65. # If no videos were found for the word or its synonyms, look for videos that match each letter of the word
  66. if not frames:
  67. for letter in word.lower(): # Convert to lowercase because file names are in lowercase.
  68. try:
  69. # Copy the video file from assets to a temporary directory
  70. file_path = f"assets/{letter}.mp4"
  71. print(f"Trying to open file: {file_path}")
  72. with asset_manager.open(file_path) as fd:
  73. temp_file_path = os.path.join(context.getCacheDir(), f"{letter}.mp4")
  74. with open(temp_file_path, 'wb') as out:
  75. out.write(fd.read())
  76. # Read video frames into a numpy array
  77. cap = cv2.VideoCapture(temp_file_path)
  78. ret, frame = cap.read()
  79. if ret:
  80. frames.append(frame)
  81. except Exception as e:
  82. print(f"Exception occurred: {e}")
  83. # If no videos were found at all, print a message and return None.
  84. if not frames:
  85. print("No match found")
  86. return None
  87. # Concatenate video frames.
  88. final_clip = np.concatenate(frames)
  89. # Export the concatenated video using cv2.VideoWriter().
  90. fourcc = cv2.VideoWriter_fourcc(*'MP4V')
  91. height, width, _ = final_clip[0].shape # get the width and height of the first frame
  92. output_file_path = os.path.join(context.getFilesDir(), 'my_concatenation.mp4')
  93. out = cv2.VideoWriter(output_file_path, fourcc, 30.0, (width, height))
  94. for frame in final_clip:
  95. out.write(frame)
  96. out.release()
  97. time.sleep(1)
  98. return output_file_path if os.path.exists(output_file_path) else None

字符串
...java

  1. package com.example.myapplication_fyppsl;
  2. import android.content.Context;
  3. import android.os.AsyncTask;
  4. import android.widget.MediaController;
  5. import android.widget.Toast;
  6. import android.widget.VideoView;
  7. import com.chaquo.python.PyObject;
  8. import com.chaquo.python.Python;
  9. import java.io.IOException;
  10. public class VideoProcessingTask extends AsyncTask<String, Void, String> {
  11. private VideoView videoView;
  12. private Context context;
  13. public VideoProcessingTask(VideoView videoView,Context context) {
  14. this.videoView = videoView;
  15. this.context = context;
  16. }
  17. @Override
  18. protected String doInBackground(String... strings) {
  19. String input = strings[0];
  20. Python py = Python.getInstance();
  21. PyObject pyf = py.getModule("displayAnimation");
  22. try {
  23. String[] files = context.getAssets().list("");
  24. for(String name : files) {
  25. System.out.println("File : " + name);
  26. }
  27. } catch (IOException e) {
  28. e.printStackTrace();
  29. }
  30. // pass the AssetManager and get back the path of created file.
  31. PyObject obj = pyf.callAttr("process_text_and_generate_video", input, context.getAssets(), context);
  32. // Check if obj is null before calling toString()
  33. return obj == null ? null : obj.toString();
  34. }
  35. @Override
  36. protected void onPostExecute(String result) {
  37. super.onPostExecute(result);
  38. // Check if result is None before trying to set the video path
  39. if (result == null) {
  40. Toast.makeText(context, "No videos found for your input.", Toast.LENGTH_SHORT).show();
  41. return;
  42. }
  43. // Set the path of the Video or URI from result of doInBackground method.
  44. videoView.setVideoPath(result);
  45. // Create a MediaController and attach it to the VideoView
  46. MediaController mediaController = new MediaController(context);
  47. mediaController.setAnchorView(videoView);
  48. // Attach the MediaController to the VideoView
  49. videoView.setMediaController(mediaController);
  50. // Start the VideoView
  51. videoView.start();
  52. }
  53. }


我试了很多方法来找出出了什么问题,但我仍然无法找到它。
1.检查所有权限
1.路径
1.视频格式
1.视频标题

  1. Gradle配置
pgky5nke

pgky5nke1#

在Java中,零参数InputStream.read()方法只返回单个字节,而不是整个文件。您可以使用如下所示的助手函数:

  1. from java import jarray, jbyte
  2. def read_stream(stream):
  3. block = jarray(jbyte)(1024 * 1024)
  4. blocks = []
  5. while True:
  6. bytes_read = stream.read(block)
  7. if bytes_read == -1:
  8. return b"".join(blocks)
  9. else:
  10. blocks.append(bytes(block)[:bytes_read])

字符串
或者,你可以把这些文件放在Python源目录中,而不是assets目录中,并把它们作为普通文件打开,如下所示:

  1. from os.path import dirname, join
  2. filename = join(dirname(__file__), "filename.txt")
  3. with open(filename, "rb") as f:
  4. ...

展开查看全部

相关问题