Deputies from the Midland County Sheriff's Office in Texas were responding to reports of an infant having breathing problems, Tuesday, when one of their vehicles got hit by a train while on the way to the scene.

According NBC-affiliated NewWest 9, the incident took place at an intersection near Highway 80 in Midland County. Two units responding to the emergency situation were forced to wait at the railroad intersection as a westbound train was halted in front of them. After seeing the deputies’ vehicles, the train pulled forward, clearing the railroad crossing, making space for the units to pass through.

Although the crossing lights were flashing, signaling the cars to wait, one of the deputies' cars drove around the lowered bar on the right side of the intersection. However, when the SUV attempted to cross the tracks, it was hit by a second train heading in the other direction. The impact of the collision caused the car to flip over.

The deputy from the second car, which was about to follow the first, rushed to the toppled car and pulled the officer out through the windshield to safety. The latter was admitted to the hospital with bruising all over his body, and pain in his neck and head.

A video of the incident was recorded by Andrea Hood-Brumfield, who was sitting in another vehicle nearby. "Oh my God!" she could be heard uttering after witnessing the crash. After she told someone to "stay in the car," she started praying for the deputy inside the crashed SUV.

Midland County Sheriff Gary Painter said the situation could have been much worse.

“When I got the call that he had been hit by a train and there was head injuries, everything runs through your head,” Painter told CBS 7. “You just, you pray for the best, but you expect for the worst. And thank God he’s not hurt that bad.”

He added that while at the hospital, the deputies were able to check on the baby, who was having breathing issues, and that the child was doing well.

Another witness, Mike Lopez, said the deputy missed getting fatally injured by seconds. “I guess he just didn’t see the other train and the train just hit him head on,” he said. “A couple more seconds and he would have been gone. Other than that, it was pretty crazy seeing it right here in front of us.”

The Department of Public Safety and the Union Pacific were investigating the crash.

Railway tracks
In this representational photo, a flooded railway track is seen in Tarboro, North Carolina, Oct. 13, 2016. Reuters