I have ruby on rails app that also uses python to provide functions to one of its features which is pose estimation. What it does is it compares the dance moves of one user, to the other dance moves of another user in a form of video. The thing is, the video is being passed by my rails app, to python script using sys.argv. After passing the video to the python script, it will do its thing with pose estimation, and the structure on how it provide the result of the dance move comparison to my rails app, is by creating a temporary file where the result data can be written. Then going back to my rails app, it will now read the content of the temporary file, then present it’s value to the user with the web app interface.
With that, I’m having an issue because in the runtime logs, it says that the file does not exist, which I assume that it’s either creating a temporary file in the deployed environment isn’t possible, or I’m just passing the incorrect directory to where the temporary file could possibly exist upon creation. Either ways, I’ll be providing the configuration of my rails app, and my python script which allows it to work in local environment but not in deployed environment(which is the the problem of mine I’m trying to raise here). I’ve been working on this project for quite a while now, and this is the last part that I need to finish, so hopefully someone can help me.
For the Configuration
This is the method in my rails controller that triggers the python script and pass the necessary video files for it to compare.
def run_script
@class_recital = ClassRecital.find(params[:id])
# Creating temp file for instructor video
temp_instructor_video = Tempfile.new(["instructor_video", ".mp4"])
File.open(temp_instructor_video.path, 'wb') do |f|
f.write(DanceClass.find(@class_recital.class_id).choreography.download)
end
instructor_video_path = temp_instructor_video.path.gsub('\\', '/')
# Creating temp file for student video
temp_student_video = Tempfile.new(["student_video", ".mp4"])
File.open(temp_student_video.path, 'wb') do |f|
f.write(@class_recital.recital.download)
end
student_video_path = temp_student_video.path.gsub('\\', '/')
python_path = "python3"
script_path = Rails.root.join("t1.py").to_s
# Execute the Python script, passing in the paths
result = `#{python_path} #{script_path} "#{instructor_video_path}" "#{student_video_path}"`
# After running the Python script, read the result from temp_result.txt
begin
temp_result_file = "/data/temp_result.txt"
performance_score = File.read("temp_result.txt").to_f.round(2)
@class_recital.update(performance_score: performance_score)
rescue => e
Rails.logger.error "Error reading temp result file: #{e.message}"
ensure
# Clean up the temp_result.txt file
File.delete("temp_result.txt") if File.exist?("temp_result.txt")
end
# Close and unlink the temp files
temp_instructor_video.close
temp_instructor_video.unlink
temp_student_video.close
temp_student_video.unlink
# Respond to the client
respond_to do |format|
format.js
format.html { redirect_to @class_recital, notice: "Analysis Completed. Check the server log for results." }
end
end
Lastly, this is the section of my python script where it receives the video files from the rails app, trigger the comparison function, then create a file to store the results so that the rails app can read it.
#.......Beginning of the code........
# Initialize video capture for the two videos to be compared
cap_instructor = cv2.VideoCapture(r"{}".format(sys.argv[1]))
cap_student = cv2.VideoCapture(r"{}".format(sys.argv[2]))
#........Rest of the code......
#lastly, file creation and writing process
result_file_path = "/data/temp_result.txt"
with open("temp_result.txt", "w") as f:
f.write(str(avg_percentage))