How can I save the output from a subprocess to a dataframe? - python

I'm working on a script to extract exif data (Latitude, Longitude, and Altitude) from RTK drone images. I have more or less copied the code below from a youtube video (Franchyze923)- with a few modifications. [I've been coding for a very short time]. How can I get the results of the subprocess to save to a table/dataframe (eventually I want to save the information to a .csv).
A different version of this script generated a .csv for every image - which I then imported all the csv files and pd.concat() them into one dataframe. That works but seems clunky.
import os
import subprocess
#Extracting exif data for images in Agisoft folder
exiftool_location = #path to exiftool.exe
images_to_extract_exif = #path to images
for path, directories, files in os.walk(images_to_extract_exif):
for images_to_extract_exif in files:
if images_to_extract_exif.endswith("JPG"):
full_jpg_path = os.path.join(path, images_to_extract_exif)
exiftool_command = [exiftool_location, "-filename", "-gpslatitude", "-gpslongitude", "-gpsaltitude", "-T", "-n", full_jpg_path]
subprocess.run(exiftool_command)
The output from the code looks great - I just have no clue how to save it to a table/dataframe.
DJI_0001.JPG 45.2405341666667 -95.3808298055556 354.427
DJI_0002.JPG 45.2405253333333 -95.3808253055556 354.434
DJI_0003.JPG 45.2404568888889 -95.3808200277778 354.447
DJI_0004.JPG 45.2403695277778 -95.3808205555556 354.431

Related

Append series of videos together in Python/OpenCV

I'm processing a video file and decided to split it up into equal chunks for parallel processing with each chunk running on its own process. I generate this series of video files that I want to connect together to make the original video.
I'm wondering what's the most efficient way of stringing these videos together without having to append frame by frame? (and ideally deleting the video files after they are read so I'm only left with one big video).
I wanted a programmatic solution oppose to a command. I found moviepy very useful for concatenating videos (its based on ffmpeg). Natsort is very useful for organizing the files by numerical order.
from moviepy.editor import VideoFileClip, concatenate_videoclips
from natsort import natsorted
#path is path to folder of videos
def concatVideos(path) :
currentVideo = None
#List all files in the directory and read points from text files one by one
for filePath in natsorted(os.listdir(path)):
if filePath.endswith(".mov"):
if currentVideo == None:
currentVideo = VideoFileClip(path + filePath)
continue
video_2 = VideoFileClip(path+filePath)
currentVideo = concatenate_videoclips([currentVideo,video_2])
currentVideo.write_videofile("export".mp4")

Convert pdf to jpg - can't see outputs

I have Python 3.6 and want to know how to convert 30+ pdf images into jpgs. I have these pdf images stored in one folder and would like to run a script to run through all the pdfs, convert them to jpgs and split them out into a new folder.
I tried to test this out on one image (see code below):
from pdf2jpg import pdf2jpg
inputpath = r"C:\Users\Admin-dsc\Documents\Image project\pdfinputs\RWG003209_2 Red.pdf"
outputpath = r"C:\Users\Admin-dsc\Documents\Image project\jpgoutputs"
result = pdf2jpg.convert_pdf2jpg(inputpath, outputpath, pages="1")
print(result)
The code runs fine, but when I look in the folder:
C:\Users\Admin-dsc\Documents\Image project\jpgoutputs
I see a folder called RWG003209_2 Red.pdf which is empty. I am confused - shouldn't the jpgs be saved here? Have I misunderstood something?

Redirecting the output of ffmpeg in subprocess

I have a folder named video_files where I have stored a bunch of video files e.g. 100.mp4, 101.mp4.... I have written a python script that iterates over each video file. Using the subprocess to call ffmpeg to extract the frames and then save the frames to the output directory named as frames. Here is my sample code for the same:
def frame_extractor(video_files_path):
video_files = sorted(glob.glob(video_files_path + "**/*.mp4", recursive=True))
print("Number of video files found: ", len(video_files))
for i, video in enumerate(video_files):
subprocess.call(["ffmpeg", "-i", video, "%04d.png"]
print("Extracted frames from all the videos")
The problem is that it extracts the frames in the present directory, from where I run this script but I want the frames to be extracted in the frames folder.
P.S: frames/%04d.png doesn't work.
Can anyone please tell me how to do this?
You could add a call after your function finishes to move all of the output files.
import os
import glob
for img in glob.glob(video_files_path + '**/*.png', recursive=True):
img_out = os.path.join('frames', os.path.split(img)[-1])
os.rename(img, img_out)

How do I get the face_recognition encoding from many images in a directory and store them in a CSV File?

This is the code I have and it works for single images:
Loading images and apply the encoding
from face_recognition.face_recognition_cli import image_files_in_folder
Image1 = face_recognition.load_image_file("Folder/Image1.jpg")
Image_encoding1 = face_recognition.face_encodings(Image1)
Image2 = face_recognition.load_image_file("Folder/Image2.jpg")
Image_encoding2 = face_recognition.face_encodings(Image2)
Face encodings are stored in the first array, after column_stack we have to resize
Encodings_For_File = np.column_stack(([Image_encoding1[0]],
[Image_encoding2[0]]))
Encodings_For_File.resize((2, 128))
Convert array to pandas dataframe and write to csv
Encodings_For_File_Panda = pd.DataFrame(Encodings_For_File)
Encodings_For_File_Panda.to_csv("Celebrity_Face_Encoding.csv")
How do I loop over the images in 'Folder' and extract the encoding into a csv file? I have to do this with many images and cannot do it manually. I tried several approaches, but none a working for me. Cv2 can be used instead of load_image_file?
Try this
Note: I am assuming you dont need to specify folder path before file name in your command. This code will show you how to iterate over the directory to list files and process them
import os
from face_recognition.face_recognition_cli import image_files_in_folder
my_dir = 'folder/path/' # Folder where all your image files reside. Ensure it ends with '/
encoding_for_file = [] # Create an empty list for saving encoded files
for i in os.listdir(my_dir): # Loop over the folder to list individual files
image = my_dir + i
image = face_recognition.load_image_file(image) # Run your load command
image_encoding = face_recognition.face_encodings(image) # Run your encoding command
encoding_for_file.append(image_encoding[0]) # Append the results to encoding_for_file list
encoding_for_file.resize((2, 128)) # Resize using your command
You can then convert to pandas and export to csv. Let me know how it goes

Python: embed images in kmz

I have a series of kmz files (1000+) within one folder that I have generated of each polygon of a feature class and a corresponding image (all images are in a separate folder). These kmz's are automatically generated from the attribute tables of my shapefiles from arcGIS. Within each kmz file I have a link to an image that corresponds to that feature as such:
<tr>
<td>Preview</td>
<td>G:\Temp\Figures\Ovr0.png</td>
</tr>
At the moment each image is but a tabular text referencing an image in the directory /Temp/Figures. What id like is to convert all those texts into links something along the lines of
<img src="file:///G:/Temp/Figures/Ovr0.png" width = 750 height=500/>
Given the large volume of files it would be ideal if this could be done within python, simplekml? On another note - at some stage I would like to share a few of these kmz files and therefore I was wondering if the best solution was to subdivide each kmz and image pair into their own respective directories and rezip the kmz file somehow?
I have managed to solve my problem by iterating each kmz and image and using the zipfile module to read the contents, rewrite the doc.kml and rezipping the files into a kmz. At the moment the images are placed after the < body >in the kmz but a more complex argument could be written with re I presume.
If there is a more efficient method please let me know...
def edit_kmz(kmz,output,image):
##Read the doc.kml file in the kmz and rewrite the doc.kml file
zf = zipfile.ZipFile(kmz)
temp = r'tempfolder\doc.kml'
for line in zf.read("doc.kml").split("\n"):
with open(temp,'a') as wf: #Create the doc.kml
if "</body>" in line:
wf.write("</body>\n<img src='files/Ovr0.png' width = 750 height=500</img>\n")
else:
wf.write('%s\n'%(line))
zf.close()
##Rezip the file
zf = zipfile.ZipFile(output,'a')
zf.write(image,arcname='files/Ovr0.png') ##Relative Path to the Image
zf.write(temp,arcname='doc.kml') ##Add revised doc.kml file
zf.close()

Categories